下面列出了java.util.regex.Pattern#CASE_INSENSITIVE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void performPreFiltering() {
condition = getFilteringText();
while (condition.startsWith(DOT_ALL_SUFFIX)) {
condition = condition.substring(2);
}
while (condition.endsWith(DOT_ALL_SUFFIX)) {
condition = condition.substring(0, condition.length() - 2);
}
patternOk = false;
int flags = 0;
if (isIgnoreCase()) {
flags = Pattern.CASE_INSENSITIVE;
}
try {
pattern = Pattern.compile(condition, flags);
patternOk = true;
} catch (PatternSyntaxException e) {
textField.setBackground(Color.RED);
}
}
public void init(InputStream in) throws ConfigurationException {
propertiesConfiguration = new PropertiesConfiguration();
propertiesConfiguration.setDelimiterParsingDisabled(true);
propertiesConfiguration.load(in, "UTF-8");
configuration = new DataConfiguration(propertiesConfiguration);
configuration.setDelimiterParsingDisabled(true);
String pa = configuration.getString(PROP_PATTERN);
int flags = 0;
flags = flags | (configuration.getBoolean(PROP_PATTERN_CANON_EQ, false) ? Pattern.CANON_EQ : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_CASE_INSENSITIVE, false) ? Pattern.CASE_INSENSITIVE : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_COMMENTS, false) ? Pattern.COMMENTS : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_DOTALL, false) ? Pattern.DOTALL : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_LITERAL, false) ? Pattern.LITERAL : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_MULTILINE, false) ? Pattern.MULTILINE : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_UNICODE_CASE, false) ? Pattern.UNICODE_CASE : 0);
flags = flags | (configuration.getBoolean(PROP_PATTERN_UNIX_LINES, false) ? Pattern.UNIX_LINES : 0);
pattern = Pattern.compile(pa, flags);
groupCount = countGroups(pattern);
name = configuration.getString(PROP_NAME, "NAME NOT SET!");
description = configuration.getString(PROP_DESCRIPTION, "DESCRIPTION NOT SET!");
testMessage = configuration.getString(PROP_TEST_MESSAGE, "");
}
private IdentifierMatcher parseRegex() throws ParseException {
StringBuilder builder = new StringBuilder();
index++; // skip initial '/'
while (!atEnd() && inRegex()) {
builder.append(current());
index++;
}
if (atEnd() || current() != '/') throw new ParseException("Unterminated regex: /" + builder.toString());
index++; // skip final '/'
int flags = 0;
while (!atEnd() && inFlags()) {
if (current() == 'i') {
flags |= Pattern.CASE_INSENSITIVE;
}
index++;
}
return Filter.createPatternMatcher(Pattern.compile(builder.toString(), flags));
}
/**
* Construct a validator that matches any one of the set of regular
* expressions with the specified case sensitivity.
*
* @param regexs
* The set of regular expressions this validator will validate
* against
* @param caseSensitive
* when <code>true</code> matching is <i>case sensitive</i>,
* otherwise matching is <i>case in-sensitive</i>
*/
public RegexValidator(String[] regexs, boolean caseSensitive) {
if (regexs == null || regexs.length == 0) {
throw new IllegalArgumentException(
"Regular expressions are missing");
}
patterns = new Pattern[regexs.length];
int flags = (caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
for (int i = 0; i < regexs.length; i++) {
if (regexs[i] == null || regexs[i].length() == 0) {
throw new IllegalArgumentException("Regular expression[" + i
+ "] is missing");
}
patterns[i] = Pattern.compile(regexs[i], flags);
}
}
private void compilePattern() {
try {
int flags = 0;
if (!myCaseSensitive) {
flags = Pattern.CASE_INSENSITIVE;
}
myPattern = Pattern.compile(myPatternString, flags);
String optimizedPattern = myPatternString;
optimizedPattern = StringUtil.trimStart(optimizedPattern, ".*");
myOptimizedIndexingPattern = Pattern.compile(optimizedPattern, flags);
}
catch (PatternSyntaxException e) {
myPattern = null;
myOptimizedIndexingPattern = null;
}
}
/**
* Construct a new regular expression filter with the specified flags case
* sensitivity.
*
* @param pattern regular string expression to match - Cannot be null
* @param caseSensitivity how to handle case sensitivity, null means
* case-sensitive
*/
public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
if (pattern == null) {
throw new IllegalArgumentException(PATTERN_IS_MISSING);
}
int flags = 0;
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
flags = Pattern.CASE_INSENSITIVE;
}
this.pattern = Pattern.compile(pattern, flags);
}
@Test
public void testStringPatternNullArgConstruction() {
try {
new RegexFileFilter((String) null, Pattern.CASE_INSENSITIVE);
fail();
} catch (final IllegalArgumentException ex) {
Assert.assertEquals(RegexFileFilter.PATTERN_IS_MISSING, ex.getMessage());
}
}
public static String flagsToString(int flags) {
StringBuilder sb = new StringBuilder();
if ((flags & Pattern.CASE_INSENSITIVE) != 0) {
sb.append("CASE_INSENSITIVE|");
}
if ((flags & Pattern.MULTILINE) != 0) {
sb.append("MULTILINE|");
}
if ((flags & Pattern.DOTALL) != 0) {
sb.append("DOTALL|");
}
if ((flags & Pattern.UNICODE_CASE) != 0) {
sb.append("UNICODE_CASE|");
}
if ((flags & Pattern.CANON_EQ) != 0) {
sb.append("CANON_EQ|");
}
if ((flags & Pattern.UNIX_LINES) != 0) {
sb.append("UNIX_LINES|");
}
if ((flags & Pattern.LITERAL) != 0) {
sb.append("LITERAL|");
}
if ((flags & Pattern.COMMENTS) != 0) {
sb.append("COMMENTS|");
}
if ((flags & UNICODE_CHARACTER_CLASS) != 0) {
sb.append("UNICODE_CHAR_CLASS|");
}
return sb.toString();
}
protected void compile(String regex) throws PatternSyntaxException {
int flags = Pattern.DOTALL;
if (caseInsensitive) {
flags |= Pattern.CASE_INSENSITIVE;
}
this.compiled = Pattern.compile(regex, flags);
}
/**
* Construct a new regular expression filter with the specified flags case sensitivity.
*
* @param pattern regular string expression to match
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the pattern is null
*/
public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
if (pattern == null) {
throw new IllegalArgumentException("Pattern is missing");
}
int flags = 0;
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
flags = Pattern.CASE_INSENSITIVE;
}
this.pattern = Pattern.compile(pattern, flags);
}
/**
* Construct a new regular expression filter with the specified flags case sensitivity.
*
* @param pattern regular string expression to match
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the pattern is null
*/
public RegexFileFilter(String pattern, IOCase caseSensitivity) {
if (pattern == null) {
throw new IllegalArgumentException("Pattern is missing");
}
int flags = 0;
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
flags = Pattern.CASE_INSENSITIVE;
}
this.pattern = Pattern.compile(pattern, flags);
}
/**
* Construct a validator that matches any one of the set of regular
* expressions with the specified case sensitivity.
*
* @param regexs The set of regular expressions this validator will
* validate against
* @param caseSensitive when <code>true</code> matching is <i>case
* sensitive</i>, otherwise matching is <i>case in-sensitive</i>
*/
public RegexValidator(String[] regexs, boolean caseSensitive) {
if (regexs == null || regexs.length == 0) {
throw new IllegalArgumentException("Regular expressions are missing");
}
patterns = new Pattern[regexs.length];
int flags = (caseSensitive ? 0: Pattern.CASE_INSENSITIVE);
for (int i = 0; i < regexs.length; i++) {
if (regexs[i] == null || regexs[i].length() == 0) {
throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
}
patterns[i] = Pattern.compile(regexs[i], flags);
}
}
/**
* Construct a new <tt>MultipleRegexFilenameFilter</tt>.
*
* @param matchType <tt>MatchType.FILENAME</tt> to match just the
* filename, <tt>MatchType.PATH</tt> to match the path
*/
public MultipleRegexFilenameFilter (MatchType matchType)
{
this.matchType = matchType;
regexOptions = Pattern.CASE_INSENSITIVE;
acceptPatterns = new ArrayList<Pattern>();
rejectPatterns = new ArrayList<Pattern>();
}
@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);
}
}
private static Pattern getPattern(final Value parg, final Value farg)
throws IllegalArgumentException {
if (debug) {
log.debug("regex pattern: " + parg);
log.debug("regex flags: " + farg);
}
//BLZG-1200 Literals with language types are not included in REGEX
if (QueryEvaluationUtil.isPlainLiteral(parg)
&& (farg == null || QueryEvaluationUtil.isPlainLiteral(farg))) {
final String ptn = ((Literal) parg).getLabel();
String flags = "";
if (farg != null) {
flags = ((Literal)farg).getLabel();
}
int f = 0;
for (char c : flags.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': {
/*
* The SPARQL REGEX operator is based on the XQuery REGEX
* operator. That operator should be Unicode clean by
* default. Therefore, when case-folding is specified, we
* also need to include the UNICODE_CASE option.
*
* @see <a
* href="https://sourceforge.net/apps/trac/bigdata/ticket/655"
* > SPARQL REGEX operator does not perform case-folding
* correctly for Unicode data </a>
*/
f |= Pattern.CASE_INSENSITIVE;
f |= Pattern.UNICODE_CASE;
break;
}
case 'x':
f |= Pattern.COMMENTS;
break;
case 'd':
f |= Pattern.UNIX_LINES;
break;
case 'u': // Implicit with 'i' flag.
// f |= Pattern.UNICODE_CASE;
break;
case 'q':
f |= Pattern.LITERAL;
break;
default:
throw new IllegalArgumentException();
}
}
final Pattern pattern = Pattern.compile(ptn, f);
return pattern;
}
throw new IllegalArgumentException();
}
/**
* 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();
}
public MatchRegexCondition(String field, String regex, boolean caseInsensitive, boolean matchPartOfValue) {
int patternFlags = caseInsensitive ? Pattern.CASE_INSENSITIVE : 0;
this.field = requireNonNull(field);
this.pattern = Pattern.compile(requireNonNull(regex), patternFlags);
this.matchingFunction = matchPartOfValue ? this::matchPartOfValue : this::matchEntireOfValue;
}
/**
* Evaluate the rule based on the context
*
* @return null if no rewrite took place
*/
public CharSequence evaluate(CharSequence url, Resolver resolver) {
Pattern pattern = this.pattern.get();
if (pattern == null) {
// Parse the pattern
int flags = 0;
if (isNocase()) {
flags |= Pattern.CASE_INSENSITIVE;
}
pattern = Pattern.compile(patternString, flags);
this.pattern.set(pattern);
}
Matcher matcher = pattern.matcher(url);
if (!matcher.matches()) {
// Evaluation done
return null;
}
// Evaluate conditions
boolean done = false;
boolean rewrite = true;
Matcher lastMatcher = null;
int pos = 0;
while (!done) {
if (pos < conditions.length) {
rewrite = conditions[pos].evaluate(matcher, lastMatcher, resolver);
if (rewrite) {
Matcher lastMatcher2 = conditions[pos].getMatcher();
if (lastMatcher2 != null) {
lastMatcher = lastMatcher2;
}
while (pos < conditions.length && conditions[pos].isOrnext()) {
pos++;
}
} else if (!conditions[pos].isOrnext()) {
done = true;
}
pos++;
} else {
done = true;
}
}
// Use the substitution to rewrite the url
if (rewrite) {
if (isEnv()) {
for (int i = 0; i < envSubstitution.size(); i++) {
envResult.get(i).set(envSubstitution.get(i).evaluate(matcher, lastMatcher, resolver));
}
}
if (isCookie()) {
cookieResult.set(cookieSubstitution.evaluate(matcher, lastMatcher, resolver));
}
if (substitution != null) {
return substitution.evaluate(matcher, lastMatcher, resolver);
} else {
return url;
}
} else {
return null;
}
}
/**
* Construct a validator that matches any one of the set of regular
* expressions with the specified case sensitivity.
*
* @param regex The regular expressions this validator will validate against
* @param caseSensitive when <code>true</code> matching is <i>case
* sensitive</i>, otherwise matching is <i>case in-sensitive</i>
*/
public RegexValidator(String regex, boolean caseSensitive) {
if (regex == null || regex.length() == 0) {
throw new IllegalArgumentException("Regular expression is missing");
}
int flags = (caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
pattern = Pattern.compile(regex, flags);
}
/**
* Create filter instance.
*
* @param caseSensitive Case sensitive flag.
* @param regex Regex search flag.
* @param ptrn String to search in string presentation of key or value.
*/
public VisorQueryScanRegexFilter(boolean caseSensitive, boolean regex, String ptrn) {
int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
this.ptrn = Pattern.compile(regex ? ptrn : ".*?" + Pattern.quote(ptrn) + ".*?", flags);
}