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

The following examples show how to use java.util.regex.Pattern#DOTALL . 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 File: SqlFunctions.java    From Quicksql with 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 File: RegularExpressionElement.java    From mongodb-async-driver with Apache License 2.0 6 votes vote down vote up
/**
 * {@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 3
Source File: WildcardFieldedTerm.java    From datawave with 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 4
Source File: SqlFunctions.java    From calcite with 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 5
Source File: RegexPredicateEval.java    From tajo with 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 6
Source File: LikePredicateEval.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
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 7
Source File: TestComparatorSerialization.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: LikePredicateEval.java    From tajo with 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 9
Source File: ExtractText.java    From localization_nifi with 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 10
Source File: ContainsTextFilter.java    From ghidra with 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 11
Source File: StartsWithTextFilter.java    From ghidra with 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 12
Source File: MatchesExactlyTextFilter.java    From ghidra with 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 13
Source File: DaoGenerator.java    From Android with 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 14
Source File: WildcardFieldedFilter.java    From datawave with 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 15
Source File: RegularExpressionElement.java    From mongodb-async-driver with Apache License 2.0 4 votes vote down vote up
/**
 * 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
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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
Source File: ReplaceBOp.java    From database with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * 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
Source File: StringBasedLikeExpression.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractBasePattern compilePatternSpec(String value) {
    return new JavaPattern(value, Pattern.DOTALL);
}
 
Example 20
Source File: RegexStringComparator.java    From hbase with 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);
}