Example usage for java.util.regex Pattern COMMENTS

List of usage examples for java.util.regex Pattern COMMENTS

Introduction

In this page you can find the example usage for java.util.regex Pattern COMMENTS.

Prototype

int COMMENTS

To view the source code for java.util.regex Pattern COMMENTS.

Click Source Link

Document

Permits whitespace and comments in pattern.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "a b";
    String patternStr = "a b";

    // Compile without comments
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Compile with comments
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches();/*from   w  w  w . j  a va 2  s.c  o  m*/

    matchFound = pattern.matches("(?x)a \t\n \\s b", inputStr);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "a b";
    String patternStr = "a b";

    // Compile without comments
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Compile with comments
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches(); // false

    // Use an inline modifier
    matchFound = pattern.matches("a b", inputStr);
    matchFound = pattern.matches("(?x)a b", inputStr);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "a b";
    String patternStr = "a b";

    // Compile without comments
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Compile with comments
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches(); // false

    // Use an inline modifier
    matchFound = pattern.matches("(?x)a \\s b", inputStr);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "a b";
    String patternStr = "a b";

    // Compile without comments
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Compile with comments
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches(); // false

    // Use an inline modifier

    matchFound = pattern.matches("a (?x:  b   )", inputStr);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Use COMMENTS but include a character class with a space

    CharSequence inputStr = "a b";
    String patternStr = "a b";

    // Compile without comments
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Compile with comments
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches();//  w  ww . j a  v  a  2 s  . c  o m

    patternStr = "a  [\\ ]  b";
    pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
    matcher = pattern.matcher(inputStr);
    matchFound = matcher.matches();
}

From source file:Main.java

/**
 * Pattern.pattern and Pattern.toString ignore any flags supplied to
 * Pattern.compile, so the regular expression you get out doesn't
 * correspond to what the Pattern was actually matching. This fixes that.
 * //from   w ww  . j  ava2 s .  c o m
 * Note that there are some flags that can't be represented.
 * 
 * FIXME: why don't we use Pattern.LITERAL instead of home-grown escaping
 * code? Is it because you can't do the reverse transformation? Should we
 * integrate that code with this?
 */
public static String toString(Pattern pattern) {
    String regex = pattern.pattern();
    final int flags = pattern.flags();
    if (flags != 0) {
        StringBuilder builder = new StringBuilder("(?");
        toStringHelper(builder, flags, Pattern.UNIX_LINES, 'd');
        toStringHelper(builder, flags, Pattern.CASE_INSENSITIVE, 'i');
        toStringHelper(builder, flags, Pattern.COMMENTS, 'x');
        toStringHelper(builder, flags, Pattern.MULTILINE, 'm');
        toStringHelper(builder, flags, Pattern.DOTALL, 's');
        toStringHelper(builder, flags, Pattern.UNICODE_CASE, 'u');
        builder.append(")");
        regex = builder.toString() + regex;
    }
    return regex;
}

From source file:com.redhat.lightblue.eval.RegexEvaluator.java

/**
 * Constructs evaluator for {field op value} style comparison
 *
 * @param expr The expression//from ww  w  .j  a  v  a 2 s  . c  o  m
 * @param md Entity metadata
 * @param context The path relative to which the expression will be
 * evaluated
 */
public RegexEvaluator(RegexMatchExpression expr, FieldTreeNode context) {
    this.relativePath = expr.getField();
    fieldMd = context.resolve(relativePath);
    if (fieldMd == null) {
        throw new EvaluationError(expr, CrudConstants.ERR_FIELD_NOT_THERE + relativePath);
    }
    int flags = 0;
    if (expr.isCaseInsensitive()) {
        flags |= Pattern.CASE_INSENSITIVE;
    }
    if (expr.isMultiline()) {
        flags |= Pattern.MULTILINE;
    }
    if (expr.isExtended()) {
        flags |= Pattern.COMMENTS;
    }
    if (expr.isDotAll()) {
        flags |= Pattern.DOTALL;
    }
    regex = Pattern.compile(expr.getRegex(), flags);
    LOGGER.debug("ctor {} {}", relativePath, regex);
}

From source file:nz.net.orcon.kanban.automation.actions.RegexAction.java

public String extract(String text, String expressionString, int match, int group, String options)
        throws IOException {

    if (text == null) {
        text = "";
    }/*from w  w w.ja v a  2 s  .  c  o  m*/

    if (expressionString == null) {
        throw new IllegalArgumentException(
                "No Regular Expression has been provided to carry out this operation.");
    }

    int optionsInEffect = 0;
    if (options != null) {
        for (String option : options.toUpperCase().split("\\|")) {
            optionsInEffect |= (option.equals("CANON_EQ")) ? Pattern.CANON_EQ
                    : (option.equals("CASE_INSENSITIVE")) ? Pattern.CASE_INSENSITIVE
                            : (option.equals("COMMENTS")) ? Pattern.COMMENTS
                                    : (option.equals("DOTALL")) ? Pattern.DOTALL
                                            : (option.equals("LITERAL")) ? Pattern.LITERAL
                                                    : (option.equals("MULTILINE")) ? Pattern.MULTILINE
                                                            : (option.equals("UNICODE_CASE"))
                                                                    ? Pattern.UNICODE_CASE
                                                                    : (option.equals("UNIX_LINES"))
                                                                            ? Pattern.UNIX_LINES
                                                                            : 0;
        }
    }

    Pattern expression = Pattern.compile(expressionString, optionsInEffect);
    Matcher matches = expression.matcher(text);

    int matchIndex = 1;
    while (matches.find()) {
        for (int groupIndex = 0; matches.groupCount() + 1 > groupIndex; groupIndex++) {
            if (matchIndex == match && groupIndex == group) {
                return matches.group(groupIndex);
            }
        }
        matchIndex++;
    }

    return "";
}

From source file:org.echocat.jomon.runtime.jaxb.PatternAdapter.java

@Nonnegative
protected int toFlags(@Nonnull String flagsAsString) {
    final Set<Character> flagsAsCharacters = newHashSet(toObject(flagsAsString.toCharArray()));
    int flags = 0;
    if (flagsAsCharacters.contains('i')) {
        flags |= Pattern.CASE_INSENSITIVE;
    }/*from   www  . j a va  2 s  .  co m*/
    if (flagsAsCharacters.contains('m')) {
        flags |= Pattern.MULTILINE;
    }
    if (flagsAsCharacters.contains('s')) {
        flags |= Pattern.DOTALL;
    }
    if (flagsAsCharacters.contains('x')) {
        flags |= Pattern.COMMENTS;
    }
    return flags;
}

From source file:org.echocat.jomon.runtime.jaxb.PatternAdapter.java

@Nonnull
protected String toFlagsAsString(@Nonnegative int flags) {
    final StringBuilder sb = new StringBuilder();
    if ((flags & Pattern.CASE_INSENSITIVE) != 0) {
        sb.append('i');
    }//from  w ww . ja v  a  2 s. co m
    if ((flags & Pattern.MULTILINE) != 0) {
        sb.append('m');
    }
    if ((flags & Pattern.DOTALL) != 0) {
        sb.append('s');
    }
    if ((flags & Pattern.COMMENTS) != 0) {
        sb.append('x');
    }
    return sb.toString();
}