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

The following examples show how to use java.util.regex.Pattern#COMMENTS . These examples are extracted from open source projects. 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
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 2
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, "");

}
 
Example 3
Source Project: RegexReplacer   File: SearchPanel.java    License: MIT License 6 votes vote down vote up
private int getRegexFlag() {
	int flag = 0;
	if (unixLinesCkb.isSelected())
		flag |= Pattern.UNIX_LINES;
	if (caseInsensitiveCkb.isSelected())
		flag |= Pattern.CASE_INSENSITIVE;
	if (commentsCkb.isSelected())
		flag |= Pattern.COMMENTS;
	if (multilineCkb.isSelected())
		flag |= Pattern.MULTILINE;
	if (literalCkb.isSelected())
		flag |= Pattern.LITERAL;
	if (dotallCkb.isSelected())
		flag |= Pattern.DOTALL;
	if (unicodeCaseCkb.isSelected())
		flag |= Pattern.UNICODE_CASE;
	if (canonEqCkb.isSelected())
		flag |= Pattern.CANON_EQ;
	return flag;
}
 
Example 4
Source Project: localization_nifi   File: ExtractText.java    License: 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 5
Source Project: Elasticsearch   File: RegexMatcher.java    License: 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 6
Source Project: Elasticsearch   File: Regex.java    License: 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 7
Source Project: Elasticsearch   File: Regex.java    License: 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 8
Source Project: lucene-solr   File: TermsComponentTest.java    License: 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 9
Source Project: nifi   File: ExtractText.java    License: 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 Project: crate   File: Regex.java    License: 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)) || ("UNICODE_CHARACTER_CLASS".equals(s))) {
            pFlags |= UNICODE_CHARACTER_CLASS;
        } else {
            throw new IllegalArgumentException("Unknown regex flag [" + s + "]");
        }
    }
    return pFlags;
}
 
Example 11
Source Project: crate   File: RegexMatcher.java    License: Apache License 2.0 5 votes vote down vote up
public static int parseFlags(@Nullable String flagsString) {
    int flags = 0;
    if (flagsString == null) {
        return flags;
    }
    for (char flag : flagsString.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;
            case ' ':
            case 'g':
                // handled in isGlobalFunction
                break;
            default:
                throw new IllegalArgumentException("The regular expression flag is unknown: " + flag);
        }
    }

    return flags;
}
 
Example 12
Source Project: ModTheSpire   File: RegexUtil.java    License: MIT License 4 votes vote down vote up
private void getSubstitutionFlags (String       substitutionCommand,
                                   String       sFlags,
                                   Substitution sub)
    throws RegexException
{
    char[] modifiers = sFlags.toCharArray();

    for (int i = 0; i < modifiers.length; i++)
    {
        char mod = modifiers[i];
        switch (mod)
        {
            case 'g':
                sub.replaceAll = true;
                break;

            case 'i':
                sub.flags |= Pattern.CASE_INSENSITIVE;
                break;

            case 'm':
                sub.flags |= Pattern.MULTILINE;
                break;

            case 'o':
                // N/A
                break;

            case 'x':
                sub.flags |= Pattern.COMMENTS;
                break;

            default:
                throw new RegexException
                    (Package.BUNDLE_NAME,
                     "RegexUtil.badSubstitutionModifier",
                     "Substitution command \"{0}\" has unknown modifier " +
                     "character\"{1}\".",
                     new Object[]
                         {
                             substitutionCommand,
                             String.valueOf (mod)
                         });
        }
    }
}
 
Example 13
/**
 * 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 14
/**
 * 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 15
Source Project: database   File: RegexBOp.java    License: 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 16
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 17
@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; //?
}