下面列出了java.util.regex.Pattern#DOTALL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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);
}
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;
}
/**
* {@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);
}
@Override
protected Pattern createPattern() {
int options = Pattern.DOTALL;
if (!caseSensitive) {
options |= Pattern.CASE_INSENSITIVE;
}
Pattern pattern = UserSearchUtils.createPattern(filterText, allowGlobbing, options);
return pattern;
}
@Override
protected Pattern createPattern() {
int options = Pattern.DOTALL;
if (!caseSensitive) {
options |= Pattern.CASE_INSENSITIVE;
}
Pattern pattern =
UserSearchUtils.createStartsWithPattern(filterText, allowGlobbing, options);
return pattern;
}
@Override
protected Pattern createPattern() {
int options = Pattern.DOTALL;
if (!caseSensitive) {
options |= Pattern.CASE_INSENSITIVE;
}
Pattern pattern = UserSearchUtils.createContainsPattern(filterText, allowGlobbing, options);
return pattern;
}
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;
}
protected void compile(String regex) throws PatternSyntaxException {
int flags = Pattern.DOTALL;
if (caseInsensitive) {
flags |= Pattern.CASE_INSENSITIVE;
}
this.compiled = Pattern.compile(regex, flags);
}
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);
}
@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);
}
}
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);
}
public static Pattern convertToRegex(String s) {
int flags = Pattern.DOTALL;
return Pattern.compile(s, flags);
}
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);
}
/**
* 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;
}
/**
* 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();
}
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;
}
/**
* 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();
}
@Override
protected AbstractBasePattern compilePatternSpec(String value) {
return new JavaPattern(value, Pattern.DOTALL);
}
/**
* 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);
}