Java Code Examples for java.util.regex.Pattern#DOTALL

The following examples show how to use java.util.regex.Pattern#DOTALL . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Quicksql   File: SqlFunctions.java    License: MIT License 6 votes vote down vote up
private static int makeRegexpFlags(String stringFlags) {
  int flags = 0;
  if (stringFlags != null) {
    for (int i = 0; i < stringFlags.length(); ++i) {
      switch (stringFlags.charAt(i)) {
      case 'i':
        flags |= Pattern.CASE_INSENSITIVE;
        break;
      case 'c':
        flags &= ~Pattern.CASE_INSENSITIVE;
        break;
      case 'n':
        flags |= Pattern.DOTALL;
        break;
      case 'm':
        flags |= Pattern.MULTILINE;
        break;
      default:
        throw RESOURCE.invalidInputForRegexpReplace(stringFlags).ex();
      }
    }
  }
  return flags;
}
 
Example 2
Source Project: datawave   File: WildcardFieldedTerm.java    License: Apache License 2.0 6 votes vote down vote up
public static Pattern convertToRegex(String s) {
    StringBuilder sb = new StringBuilder();
    char[] chars = s.toCharArray();
    
    int lastIndex = chars.length - 1;
    for (int x = 0; x < chars.length; x++) {
        char currChar = chars[x];
        
        if (currChar == '\\' && x < lastIndex && (chars[x + 1] == '*' || chars[x + 1] == '?' || chars[x + 1] == '\\')) {
            x++;
            sb.append("\\");
            sb.append(chars[x]);
        } else if (currChar == '*') {
            sb.append(".*?");
        } else if (currChar == '?') {
            sb.append(".");
        } else {
            sb.append(currChar);
        }
    }
    
    int flags = Pattern.DOTALL;
    return Pattern.compile(sb.toString(), flags);
}
 
Example 3
Source Project: calcite   File: SqlFunctions.java    License: Apache License 2.0 6 votes vote down vote up
private static int makeRegexpFlags(String stringFlags) {
  int flags = 0;
  if (stringFlags != null) {
    for (int i = 0; i < stringFlags.length(); ++i) {
      switch (stringFlags.charAt(i)) {
      case 'i':
        flags |= Pattern.CASE_INSENSITIVE;
        break;
      case 'c':
        flags &= ~Pattern.CASE_INSENSITIVE;
        break;
      case 'n':
        flags |= Pattern.DOTALL;
        break;
      case 'm':
        flags |= Pattern.MULTILINE;
        break;
      default:
        throw RESOURCE.invalidInputForRegexpReplace(stringFlags).ex();
      }
    }
  }
  return flags;
}
 
Example 4
/**
 * {@inheritDoc}
 * <p>
 * Returns the {@link Pattern}.
 * </p>
 */
@Override
public Pattern getValueAsObject() {

    int options = 0;
    if ((myOptions & CASE_INSENSITIVE) == CASE_INSENSITIVE) {
        options |= Pattern.CASE_INSENSITIVE;
    }
    if ((myOptions & MULTILINE) == MULTILINE) {
        options |= Pattern.MULTILINE;
    }
    if ((myOptions & DOT_ALL) == DOT_ALL) {
        options |= Pattern.DOTALL;
    }
    if ((myOptions & UNICODE) == UNICODE) {
        options |= PATTERN_UNICODE;
    }

    return Pattern.compile(myPattern, options);
}
 
Example 5
Source Project: ghidra   File: MatchesExactlyTextFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Pattern createPattern() {
	int options = Pattern.DOTALL;
	if (!caseSensitive) {
		options |= Pattern.CASE_INSENSITIVE;
	}

	Pattern pattern = UserSearchUtils.createPattern(filterText, allowGlobbing, options);
	return pattern;
}
 
Example 6
Source Project: ghidra   File: StartsWithTextFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Pattern createPattern() {
	int options = Pattern.DOTALL;
	if (!caseSensitive) {
		options |= Pattern.CASE_INSENSITIVE;
	}

	Pattern pattern =
		UserSearchUtils.createStartsWithPattern(filterText, allowGlobbing, options);
	return pattern;
}
 
Example 7
Source Project: ghidra   File: ContainsTextFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Pattern createPattern() {

	int options = Pattern.DOTALL;
	if (!caseSensitive) {
		options |= Pattern.CASE_INSENSITIVE;
	}

	Pattern pattern = UserSearchUtils.createContainsPattern(filterText, allowGlobbing, options);
	return pattern;
}
 
Example 8
Source Project: localization_nifi   File: ExtractText.java    License: Apache License 2.0 5 votes vote down vote up
int getCompileFlags(ProcessContext context) {
    int flags = (context.getProperty(UNIX_LINES).asBoolean() ? Pattern.UNIX_LINES : 0)
            | (context.getProperty(CASE_INSENSITIVE).asBoolean() ? Pattern.CASE_INSENSITIVE : 0)
            | (context.getProperty(COMMENTS).asBoolean() ? Pattern.COMMENTS : 0)
            | (context.getProperty(MULTILINE).asBoolean() ? Pattern.MULTILINE : 0)
            | (context.getProperty(LITERAL).asBoolean() ? Pattern.LITERAL : 0)
            | (context.getProperty(DOTALL).asBoolean() ? Pattern.DOTALL : 0)
            | (context.getProperty(UNICODE_CASE).asBoolean() ? Pattern.UNICODE_CASE : 0)
            | (context.getProperty(CANON_EQ).asBoolean() ? Pattern.CANON_EQ : 0)
            | (context.getProperty(UNICODE_CHARACTER_CLASS).asBoolean() ? Pattern.UNICODE_CHARACTER_CLASS : 0);
    return flags;
}
 
Example 9
Source Project: tajo   File: RegexPredicateEval.java    License: Apache License 2.0 5 votes vote down vote up
protected void compile(String regex) throws PatternSyntaxException {
  int flags = Pattern.DOTALL;
  if (caseInsensitive) {
    flags |= Pattern.CASE_INSENSITIVE;
  }
  this.compiled = Pattern.compile(regex, flags);
}
 
Example 10
Source Project: tajo   File: LikePredicateEval.java    License: Apache License 2.0 5 votes vote down vote up
protected void compile(String pattern) throws PatternSyntaxException {
  String escaped = StringUtils.escapeRegexp(pattern);
  String regex = escaped.replace("_", ".").replace("%", ".*");
  int flags = Pattern.DOTALL;
  if (caseInsensitive) {
    flags |= Pattern.CASE_INSENSITIVE;
  }
  this.compiled = Pattern.compile(regex, flags);
}
 
Example 11
@Test
public void testRegexStringComparator() throws Exception {
  // test without specifying flags
  RegexStringComparator regexStringComparator = new RegexStringComparator(".+-2");
  assertTrue(regexStringComparator.areSerializedFieldsEqual(
    ProtobufUtil.toComparator(ProtobufUtil.toComparator(regexStringComparator))));

  // test with specifying flags
  try {
    new RegexStringComparator("regex", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  } catch (Throwable t) {
    assertNull("Exception occurred while created the RegexStringComparator object", t);
  }
}
 
Example 12
protected void compile(String pattern) throws PatternSyntaxException {
  String escaped = escapeRegexpForLike(pattern);
  String regex = escaped.replace("_", ".").replace("%", ".*");
  int flags = Pattern.DOTALL;
  if (caseInsensitive) {
    flags |= Pattern.CASE_INSENSITIVE;
  }
  this.compiled = Pattern.compile(regex, flags);
}
 
Example 13
Source Project: datawave   File: WildcardFieldedFilter.java    License: Apache License 2.0 4 votes vote down vote up
public static Pattern convertToRegex(String s) {
    int flags = Pattern.DOTALL;
    return Pattern.compile(s, flags);
}
 
Example 14
Source Project: Android   File: DaoGenerator.java    License: MIT License 4 votes vote down vote up
private Pattern compilePattern(String sectionName) {
    int flags = Pattern.DOTALL | Pattern.MULTILINE;
    return Pattern.compile(".*^\\s*?//\\s*?KEEP " + sectionName + ".*?\n(.*?)^\\s*// KEEP " + sectionName
            + " END.*?\n", flags);
}
 
Example 15
/**
 * Converts the {@link Pattern#flags() pattern flags} into a options value.
 * <p>
 * Note that the {@link #VERBOSE} and {@link #LOCALE_DEPENDENT} do not have
 * {@link Pattern} equivalent flags.
 * </p>
 * <p>
 * <blockquote>
 *
 * <pre>
 * {@link Pattern#CASE_INSENSITIVE} ==> {@link #CASE_INSENSITIVE}
 * {@link Pattern#MULTILINE} ==> {@link #MULTILINE}
 * {@link Pattern#DOTALL} ==> {@link #DOT_ALL}
 * {@link Pattern#UNICODE_CHARACTER_CLASS} ==> {@link #UNICODE}
 * </pre>
 *
 * </blockquote>
 *
 * @param pattern
 *            The pattern to extract the options from.
 * @return The options integer value.
 */
protected static int optionsAsInt(final Pattern pattern) {
    int optInt = 0;

    if (pattern != null) {
        final int flags = pattern.flags();
        if ((flags & Pattern.CASE_INSENSITIVE) == Pattern.CASE_INSENSITIVE) {
            optInt |= CASE_INSENSITIVE;
        }
        if ((flags & Pattern.MULTILINE) == Pattern.MULTILINE) {
            optInt |= MULTILINE;
        }
        if ((flags & Pattern.DOTALL) == Pattern.DOTALL) {
            optInt |= DOT_ALL;
        }
        if ((flags & PATTERN_UNICODE) == PATTERN_UNICODE) {
            optInt |= UNICODE;
        }
    }

    return optInt;
}
 
Example 16
/**
 * Determines whether the two operands match according to the <code>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Regex node, BindingSet bindings)
		throws QueryEvaluationException {
	Value arg = evaluate(node.getArg(), bindings);
	Value parg = evaluate(node.getPatternArg(), bindings);
	Value farg = null;
	ValueExpr flagsArg = node.getFlagsArg();
	if (flagsArg != null) {
		farg = evaluate(flagsArg, bindings);
	}

	if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
			&& (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
		String text = ((Literal) arg).getLabel();
		String ptn = ((Literal) parg).getLabel();
		String flags = "";
		if (farg != null) {
			flags = ((Literal) farg).getLabel();
		}
		// TODO should this Pattern be cached?
		int f = 0;
		for (char c : flags.toCharArray()) {
			switch (c) {
			case 's':
				f |= Pattern.DOTALL;
				break;
			case 'm':
				f |= Pattern.MULTILINE;
				break;
			case 'i':
				f |= Pattern.CASE_INSENSITIVE;
				f |= Pattern.UNICODE_CASE;
				break;
			case 'x':
				f |= Pattern.COMMENTS;
				break;
			case 'd':
				f |= Pattern.UNIX_LINES;
				break;
			case 'u':
				f |= Pattern.UNICODE_CASE;
				break;
			case 'q':
				f |= Pattern.LITERAL;
				break;
			default:
				throw new ValueExprEvaluationException(flags);
			}
		}
		Pattern pattern = Pattern.compile(ptn, f);
		boolean result = pattern.matcher(text).find();
		return BooleanLiteral.valueOf(result);
	}

	throw new ValueExprEvaluationException();
}
 
Example 17
private static Pattern getPattern(final Value pattern, final Value flags) 
		throws IllegalArgumentException {
	
	if (!QueryEvaluationUtil.isSimpleLiteral(pattern)) {
		throw new IllegalArgumentException(
				"incompatible operand for REPLACE: " + pattern);
	}

	String flagString = null;
	if (flags != null) {
		if (!QueryEvaluationUtil.isSimpleLiteral(flags)) {
			throw new IllegalArgumentException(
					"incompatible operand for REPLACE: " + flags);
		}
		flagString = ((Literal) flags).getLabel();
	}

	String patternString = ((Literal) pattern).getLabel();

	int f = 0;
	if (flagString != null) {
		for (char c : flagString.toCharArray()) {
               // See https://www.w3.org/TR/xpath-functions/#flags
			switch (c) {
			case 's':
				f |= Pattern.DOTALL;
				break;
			case 'm':
				f |= Pattern.MULTILINE;
				break;
			case 'i':
				f |= Pattern.CASE_INSENSITIVE;
				break;
			case 'x':
				f |= Pattern.COMMENTS;
				break;
			case 'd':
				f |= Pattern.UNIX_LINES;
				break;
			case 'u':
				f |= Pattern.UNICODE_CASE;
				break;
			case 'q':
				f |= Pattern.LITERAL;
				break;
			default:
				throw new IllegalArgumentException(flagString);
			}
		}
	}

	Pattern p = Pattern.compile(patternString, f);
	
	return p;
	
}
 
Example 18
/**
 * Determines whether the two operands match according to the <code>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the
 * <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
private Value evaluate(Regex node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value arg = evaluate(node.getArg(), bindings);
    Value parg = evaluate(node.getPatternArg(), bindings);
    Value farg = null;
    ValueExpr flagsArg = node.getFlagsArg();
    if (flagsArg != null) {
        farg = evaluate(flagsArg, bindings);
    }
    if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
            && (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
        String text = ((Literal) arg).getLabel();
        String ptn = ((Literal) parg).getLabel();
        String flags = "";
        if (farg != null) {
            flags = ((Literal) farg).getLabel();
        }
        // TODO should this Pattern be cached?
        int f = 0;
        for (char c : flags.toCharArray()) {
            switch (c) {
                case 's':
                    f |= Pattern.DOTALL;
                    break;
                case 'm':
                    f |= Pattern.MULTILINE;
                    break;
                case 'i':
                    f |= Pattern.CASE_INSENSITIVE;
                    f |= Pattern.UNICODE_CASE;
                    break;
                case 'x':
                    f |= Pattern.COMMENTS;
                    break;
                case 'd':
                    f |= Pattern.UNIX_LINES;
                    break;
                case 'u':
                    f |= Pattern.UNICODE_CASE;
                    break;
                default:
                    throw new ValueExprEvaluationException(flags);
            }
        }
        Pattern pattern = Pattern.compile(ptn, f);
        boolean result = pattern.matcher(text).find();
        return BooleanLiteral.valueOf(result);
    }
    throw new ValueExprEvaluationException();
}
 
Example 19
@Override
protected AbstractBasePattern compilePatternSpec(String value) {
    return new JavaPattern(value, Pattern.DOTALL);
}
 
Example 20
Source Project: hbase   File: RegexStringComparator.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Constructor
 * Adds Pattern.DOTALL to the underlying Pattern
 * @param expr a valid regular expression
 * @param engine engine implementation type
 */
public RegexStringComparator(String expr, EngineType engine) {
  this(expr, Pattern.DOTALL, engine);
}