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

The following examples show how to use java.util.regex.Pattern#MULTILINE . 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: RegExpParser.java    From es6draft with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: PatternConstructor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: TypeConverter.java    From opentest with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: FindSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 5
Source File: TRegex.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 6
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 7
Source File: EcmaPattern.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 8
Source File: TestActionNGTest.java    From opentest with MIT License 5 votes vote down vote up
@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);
}
 
Example 9
Source File: RegexMatcher.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: Regex.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: Regex.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 12
Source File: TermsComponentTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example 13
Source File: TaskTags.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: ExtractText.java    From 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 15
Source File: BirtComp.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @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 );
	}
}
 
Example 16
Source File: RegexBOp.java    From database with GNU General Public License v2.0 4 votes vote down vote up
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();
        
    }
 
Example 17
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 18
Source File: RegExpAvm2Item.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
@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; //?
}
 
Example 19
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 20
Source File: JavaCode.java    From RADL with Apache License 2.0 4 votes vote down vote up
private boolean isSingleLinePattern(Pattern pattern) {
  return (pattern.flags() & Pattern.MULTILINE) == 0;
}