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:org.apache.nifi.processors.standard.EvaluateRegularExpression.java

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;
}

From source file:com.parse.OfflineQueryLogic.java

/**
 * Matches $regex constraints.// w w w.  j a v a  2 s . co m
 */
private static boolean matchesRegexConstraint(Object constraint, Object value, String options)
        throws ParseException {
    if (value == null || value == JSONObject.NULL) {
        return false;
    }

    if (options == null) {
        options = "";
    }

    if (!options.matches("^[imxs]*$")) {
        throw new ParseException(ParseException.INVALID_QUERY,
                String.format("Invalid regex options: %s", options));
    }

    int flags = 0;
    if (options.contains("i")) {
        flags = flags | Pattern.CASE_INSENSITIVE;
    }
    if (options.contains("m")) {
        flags = flags | Pattern.MULTILINE;
    }
    if (options.contains("x")) {
        flags = flags | Pattern.COMMENTS;
    }
    if (options.contains("s")) {
        flags = flags | Pattern.DOTALL;
    }

    String regex = (String) constraint;
    Pattern pattern = Pattern.compile(regex, flags);
    Matcher matcher = pattern.matcher((String) value);
    return matcher.find();
}

From source file:org.opennms.netmgt.collectd.HttpCollector.java

private static List<HttpCollectionAttribute> processResponse(final Locale responseLocale,
        final String responseBodyAsString, final HttpCollectionSet collectionSet,
        HttpCollectionResource collectionResource) {
    LOG.debug("processResponse:");
    LOG.debug("responseBody = {}", responseBodyAsString);
    LOG.debug("getmatches = {}", collectionSet.getUriDef().getUrl().getMatches());
    List<HttpCollectionAttribute> butes = new LinkedList<HttpCollectionAttribute>();
    int flags = 0;
    if (collectionSet.getUriDef().getUrl().getCanonicalEquivalence()) {
        flags |= Pattern.CANON_EQ;
    }//  w  w w .  j a v  a 2  s .c  om
    if (collectionSet.getUriDef().getUrl().getCaseInsensitive()) {
        flags |= Pattern.CASE_INSENSITIVE;
    }
    if (collectionSet.getUriDef().getUrl().getComments()) {
        flags |= Pattern.COMMENTS;
    }
    if (collectionSet.getUriDef().getUrl().getDotall()) {
        flags |= Pattern.DOTALL;
    }
    if (collectionSet.getUriDef().getUrl().getLiteral()) {
        flags |= Pattern.LITERAL;
    }
    if (collectionSet.getUriDef().getUrl().getMultiline()) {
        flags |= Pattern.MULTILINE;
    }
    if (collectionSet.getUriDef().getUrl().getUnicodeCase()) {
        flags |= Pattern.UNICODE_CASE;
    }
    if (collectionSet.getUriDef().getUrl().getUnixLines()) {
        flags |= Pattern.UNIX_LINES;
    }
    LOG.debug("flags = {}", flags);
    Pattern p = Pattern.compile(collectionSet.getUriDef().getUrl().getMatches(), flags);
    Matcher m = p.matcher(responseBodyAsString);

    final boolean matches = m.matches();
    if (matches) {
        LOG.debug("processResponse: found matching attributes: {}", matches);
        final List<Attrib> attribDefs = collectionSet.getUriDef().getAttributes().getAttribCollection();
        final AttributeGroupType groupType = new AttributeGroupType(collectionSet.getUriDef().getName(),
                AttributeGroupType.IF_TYPE_ALL);

        final List<Locale> locales = new ArrayList<Locale>();
        if (responseLocale != null) {
            locales.add(responseLocale);
        }
        locales.add(Locale.getDefault());
        if (Locale.getDefault() != Locale.ENGLISH) {
            locales.add(Locale.ENGLISH);
        }

        for (final Attrib attribDef : attribDefs) {
            final String type = attribDef.getType();
            String value = null;
            try {
                value = m.group(attribDef.getMatchGroup());
            } catch (final IndexOutOfBoundsException e) {
                LOG.error(
                        "IndexOutOfBoundsException thrown while trying to find regex group, your regex does not contain the following group index: {}",
                        attribDef.getMatchGroup());
                LOG.error("Regex statement: {}", collectionSet.getUriDef().getUrl().getMatches());
                continue;
            }

            if (!type.matches("^([Oo](ctet|CTET)[Ss](tring|TRING))|([Ss](tring|TRING))$")) {
                Number num = null;
                for (final Locale locale : locales) {
                    try {
                        num = NumberFormat.getNumberInstance(locale).parse(value);
                        LOG.debug("processResponse: found a parsable number with locale \"{}\".", locale);
                        break;
                    } catch (final ParseException e) {
                        LOG.warn(
                                "attribute {} failed to match a parsable number with locale \"{}\"! Matched \"{}\" instead.",
                                attribDef.getAlias(), locale, value);
                    }
                }

                if (num == null) {
                    LOG.warn("processResponse: gave up attempting to parse numeric value, skipping group {}",
                            attribDef.getMatchGroup());
                    continue;
                }

                final HttpCollectionAttribute bute = new HttpCollectionAttribute(collectionResource,
                        new HttpCollectionAttributeType(attribDef, groupType), num);
                LOG.debug("processResponse: adding found numeric attribute: {}", bute);
                butes.add(bute);
            } else {
                HttpCollectionAttribute bute = new HttpCollectionAttribute(collectionResource,
                        new HttpCollectionAttributeType(attribDef, groupType), value);
                LOG.debug("processResponse: adding found string attribute: {}", bute);
                butes.add(bute);
            }
        }
    } else {
        LOG.debug("processResponse: found matching attributes: {}", matches);
    }
    return butes;
}

From source file:org.eclipse.californium.proxy.HttpTranslator.java

static boolean looksLikeUTF8(byte[] utf8) {
    boolean response = false;
    Pattern p = Pattern.compile("\\A(\n" + "  [\\x09\\x0A\\x0D\\x20-\\x7E]             # ASCII\\n"
            + "| [\\xC2-\\xDF][\\x80-\\xBF]               # non-overlong 2-byte\n"
            + "|  \\xE0[\\xA0-\\xBF][\\x80-\\xBF]         # excluding overlongs\n"
            + "| [\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2}  # straight 3-byte\n"
            + "|  \\xED[\\x80-\\x9F][\\x80-\\xBF]         # excluding surrogates\n"
            + "|  \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2}      # planes 1-3\n"
            + "| [\\xF1-\\xF3][\\x80-\\xBF]{3}            # planes 4-15\n"
            + "|  \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2}      # plane 16\n" + ")*\\z", Pattern.COMMENTS);

    String phonyString;/*ww  w.  j  a v a  2s .com*/
    try {
        phonyString = new String(utf8, "ISO-8859-1");
        response = p.matcher(phonyString).matches();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        response = false;
    }
    return response;
}