下面列出了java.util.regex.Pattern#MULTILINE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int toPatternFlags(int mask) {
int flags = 0;
if ((mask & Flags.IGNORE_CASE) != 0) {
flags |= Pattern.CASE_INSENSITIVE;
}
if ((mask & Flags.UNICODE) != 0) {
flags |= Pattern.UNICODE_CASE;
}
if ((mask & Flags.MULTILINE) != 0) {
flags |= Pattern.MULTILINE;
}
if ((mask & Flags.DOTALL) != 0) {
flags |= Pattern.DOTALL;
}
return flags;
}
private static int parseOptionFlags(String opts) {
int flags = 0;
for(int i = 0; i < opts.length(); ++i) {
switch(opts.charAt(i)) {
// Standard 'special construct match flags'
case 'i': flags |= Pattern.CASE_INSENSITIVE; break;
case 'd': flags |= Pattern.UNIX_LINES; break;
case 'm': flags |= Pattern.MULTILINE; break;
case 's': flags |= Pattern.DOTALL; break;
case 'u': flags |= Pattern.UNICODE_CASE; break;
case 'x': flags |= Pattern.COMMENTS; break;
// And pick a letters for remaining flags
case 'l': flags |= Pattern.LITERAL; break;
case 'c': flags |= Pattern.CANON_EQ; break;
default:
throw new InvalidParameterValueException("Invalid option: " + opts.charAt(i));
}
}
return flags;
}
void updatePattern() {
reset();
String p = bar.getPattern();
if (!bar.getRegularExpression()) {
p = Pattern.quote(p);
if (bar.getWholeWords()) {
p="\\b"+p+"\\b"; // NOI18N
}
}
int flags = Pattern.MULTILINE;
if (!bar.getMatchCase()) {
flags |= Pattern.CASE_INSENSITIVE;
}
try {
pattern = Pattern.compile(p, flags);
} catch (PatternSyntaxException psex) {
String message = NbBundle.getMessage(FindSupport.class, "FindBar.invalidExpression"); // NOI18N
StatusDisplayer.getDefault().setStatusText(message, StatusDisplayer.IMPORTANCE_FIND_OR_REPLACE);
}
findNext();
if (bar.getHighlightResults()) {
highlight(tc, false);
}
}
/**
* Converts a string containing JS regex flag characters into an integer
* with the corresponding bits set, according to the Pattern enum.
*/
private static int parseRegexFlags(String flagsString) {
int regexFlags = 0;
if (flagsString.contains("s")) {
regexFlags |= Pattern.DOTALL;
}
if (flagsString.contains("i")) {
regexFlags |= Pattern.CASE_INSENSITIVE;
}
if (flagsString.contains("m")) {
regexFlags |= Pattern.MULTILINE;
}
return regexFlags;
}
/**
* Creates a pattern element from the pattern string which is either a reg-ex expression or in
* our old 'StringMatcher' format.
*
* @param pattern The search pattern
* @param isRegex <code>true</code> if the passed string already is a reg-ex pattern
* @param isStringMatcher <code>true</code> if the passed string is in the StringMatcher format.
* @param isCaseSensitive Set to <code>true</code> to create a case insensitive pattern
* @param isWholeWord <code>true</code> to create a pattern that requires a word boundary at the
* beginning and the end.
* @return The created pattern
* @throws PatternSyntaxException if "\R" is at an illegal position
*/
public static Pattern createPattern(String pattern, boolean isRegex, boolean isStringMatcher, boolean isCaseSensitive, boolean isWholeWord) throws PatternSyntaxException {
if (isRegex) {
pattern= substituteLinebreak(pattern);
Assert.isTrue(!isWholeWord, "isWholeWord unsupported together with isRegex"); //$NON-NLS-1$
} else {
int len= pattern.length();
StringBuilder buffer= new StringBuilder(len + 10);
// don't add a word boundary if the search text does not start with
// a word char. (this works around a user input error).
if (isWholeWord && len > 0 && isWordChar(pattern.charAt(0))) {
buffer.append("\\b"); //$NON-NLS-1$
}
appendAsRegEx(isStringMatcher, pattern, buffer);
if (isWholeWord && len > 0 && isWordChar(pattern.charAt(len - 1))) {
buffer.append("\\b"); //$NON-NLS-1$
}
pattern= buffer.toString();
}
int regexOptions= Pattern.MULTILINE;
if (!isCaseSensitive) {
regexOptions|= Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
}
return Pattern.compile(pattern, regexOptions);
}
public Pattern toPattern() {
if (pattern == null) {
int flags = Pattern.MULTILINE;
if (!caseSensitive) {
flags = flags | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
}
StringBuilder builder = new StringBuilder();
builder.append("^.*((");
String tagNames = taskTags.stream().map(it -> Pattern.quote(it.getName())).collect(joining("|"));
builder.append(tagNames);
builder.append(")(.*)?)$");
pattern = Pattern.compile(builder.toString(), flags);
}
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;
}
private static EcmaPattern ecmaToJavaRegexp(String ecmaPattern, String options) {
int flags = 0;
if ( options.contains("i") ) {
flags |= Pattern.CASE_INSENSITIVE;
}
if ( options.contains("m") ) {
flags |= Pattern.MULTILINE;
}
return new EcmaPattern(Pattern.compile(ecmaPattern, flags));
}
@Test
public void testReadRegexArgument_String() {
TestAction instance = new TestActionImpl();
instance.writeArgument("regex1", "/aaa\\d{3}bbb/sim");
Pattern result3 = instance.readRegexArgument("regex1");
int expectedFlags = Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE;
assertTrue(result3.flags() == expectedFlags);
assertTrue(result3.pattern().equals("aaa\\d{3}bbb"));
assertTrue(result3.matcher("aaa123bbb").find() == true);
assertTrue(result3.matcher("aaa12bbb").find() == false);
}
public static int parseFlags(@Nullable BytesRef flagsString) {
int flags = 0;
if (flagsString == null) {
return flags;
}
for (char flag : flagsString.utf8ToString().toCharArray()) {
switch (flag) {
case 'i':
flags = flags | Pattern.CASE_INSENSITIVE;
break;
case 'u':
flags = flags | Pattern.UNICODE_CASE;
break;
case 'U':
flags = flags | Pattern.UNICODE_CHARACTER_CLASS;
break;
case 's':
flags = flags | Pattern.DOTALL;
break;
case 'm':
flags = flags | Pattern.MULTILINE;
break;
case 'x':
flags = flags | Pattern.COMMENTS;
break;
case 'd':
flags = flags | Pattern.UNIX_LINES;
break;
default:
break;
}
}
return flags;
}
public static int flagsFromString(String flags) {
int pFlags = 0;
for (String s : Strings.delimitedListToStringArray(flags, "|")) {
if (s.isEmpty()) {
continue;
}
s = s.toUpperCase(Locale.ROOT);
if ("CASE_INSENSITIVE".equals(s)) {
pFlags |= Pattern.CASE_INSENSITIVE;
} else if ("MULTILINE".equals(s)) {
pFlags |= Pattern.MULTILINE;
} else if ("DOTALL".equals(s)) {
pFlags |= Pattern.DOTALL;
} else if ("UNICODE_CASE".equals(s)) {
pFlags |= Pattern.UNICODE_CASE;
} else if ("CANON_EQ".equals(s)) {
pFlags |= Pattern.CANON_EQ;
} else if ("UNIX_LINES".equals(s)) {
pFlags |= Pattern.UNIX_LINES;
} else if ("LITERAL".equals(s)) {
pFlags |= Pattern.LITERAL;
} else if ("COMMENTS".equals(s)) {
pFlags |= Pattern.COMMENTS;
} else if ("UNICODE_CHAR_CLASS".equals(s)) {
pFlags |= UNICODE_CHARACTER_CLASS;
} else {
throw new IllegalArgumentException("Unknown regex flag [" + s + "]");
}
}
return pFlags;
}
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();
}
@Test
public void testRegexpFlagParsing() {
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(TermsParams.TERMS_REGEXP_FLAG, "case_insensitive", "literal", "comments", "multiline", "unix_lines",
"unicode_case", "dotall", "canon_eq");
try (TermsComponent termsComponent = new TermsComponent()) {
int flags = termsComponent.resolveRegexpFlags(params);
int expected = Pattern.CASE_INSENSITIVE | Pattern.LITERAL | Pattern.COMMENTS | Pattern.MULTILINE | Pattern.UNIX_LINES
| Pattern.UNICODE_CASE | Pattern.DOTALL | Pattern.CANON_EQ;
assertEquals(expected, flags);
} catch (IOException e) {
fail("Error closing TermsComponent");
}
}
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;
}
@Override
public Object call(String methodName, List<Object> args) {
int flags = 0;
for (char c : modifier.toCharArray()) {
switch (c) {
case 'g':
//global (??)
break;
case 'i':
flags |= Pattern.CASE_INSENSITIVE;
break;
case 's':
flags |= Pattern.DOTALL;
break;
case 'm':
flags |= Pattern.MULTILINE;
break;
case 'x':
flags |= Pattern.COMMENTS; //?
break;
default:
//?
break;
}
}
Pattern p = Pattern.compile(pattern, flags);
switch (methodName) {
case "exec":
String estr = EcmaScript.toString(args.get(0));
Matcher m = p.matcher(estr);
if (m.find()) {
List<Object> avals = new ArrayList<>();
for (int i = 0; i <= m.groupCount(); i++) {
avals.add(m.group(i));
}
ArrayType a = new ArrayType(avals);
a.setAttribute("input", estr);
a.setAttribute("index", m.start());
return a;
} else {
return Null.INSTANCE;
}
case "test":
String tstr = EcmaScript.toString(args.get(0));
return p.matcher(tstr).find(); //boolean
}
return Undefined.INSTANCE; //?
}
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();
}
/**
* @param obj1
* @param obj2
* @return true x matches Javascript pattern y
* @throws BirtException
* @throws DataException
*/
private static boolean match( Object obj1, Object obj2 )
throws BirtException
{
if ( obj2 == null )
{
return false;
}
if ( obj1 == null )
{
return false;
}
String sourceStr = obj1.toString( );
String pattern = obj2.toString( );
// Pattern can be one of the following:
// (1)Java regular expression pattern
// (2)JavaScript RegExp construction syntax: "/RegExpr/[flags]", where flags
// can be a combination of 'g', 'm', 'i'
Matcher jsReExprMatcher = getJSReExprPatternMatcher( pattern );
int flags = 0;
if ( jsReExprMatcher.matches( ) )
{
// This is a Javascript syntax
// Get the flags; we only expect "m", "i", "g"
String flagStr = pattern.substring( jsReExprMatcher.start( 2 ),
jsReExprMatcher.end( 2 ) );
for ( int i = 0; i < flagStr.length( ); i++ )
{
switch ( flagStr.charAt( i ) )
{
case 'm' :
flags |= Pattern.MULTILINE;
break;
case 'i' :
flags |= Pattern.CASE_INSENSITIVE;
break;
case 'g' :
break; // this flag has no effect
default :
throw new BirtException( PLUGIN_ID,
ResourceConstants.INVALID_REGULAR_EXPRESSION,
pattern );
}
}
pattern = pattern.substring( jsReExprMatcher.start( 1 ),
jsReExprMatcher.end( 1 ) );
}
try
{
Matcher m = Pattern.compile( pattern, flags ).matcher( sourceStr );
return m.find( );
}
catch ( PatternSyntaxException e )
{
throw new BirtException( PLUGIN_ID,
ResourceConstants.INVALID_REGULAR_EXPRESSION,
e );
}
}
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);
}
/**
* 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 boolean isSingleLinePattern(Pattern pattern) {
return (pattern.flags() & Pattern.MULTILINE) == 0;
}