Example usage for java.util.regex Pattern pattern

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

Introduction

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

Prototype

pattern

Source Link

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    // recycle the pattern
    String tmp = p.pattern();
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();/*  www.ja  v a2s . com*/
    System.out.println("second pattern matches " + isOk);
}

From source file:Main.java

public static void main(String args[]) {
    // match a single digit
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    // recycle the pattern
    String tmp = p.pattern();
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();//from  w w  w . j ava 2  s  .  co m
    System.out.println("second pattern matches " + isOk);
}

From source file:org.openecomp.sdc.be.model.operations.impl.PropertyOperation.java

public static void main(String[] args) {

    List<Pattern> buildFunctionPatterns = buildFunctionPatterns();

    for (Pattern pattern : buildFunctionPatterns) {

        String[] strs = { "str_replace", "{ str_replace:", " {str_replace:", " {   str_replace:",
                "{str_replace:" };
        for (String str : strs) {
            Matcher m = pattern.matcher(str);
            System.out.println(pattern.pattern() + " " + str + " " + m.find());
        }/*w  w  w .j av a2  s  .  co m*/
    }

}

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  ww  w  .  j a v  a2 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:PatternMethodExample.java

public static void reusePatternMethodExample() {
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    String tmp = p.pattern();
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();//from w w  w .j  av  a 2s. c  o  m
    System.out.println("second pattern matches " + isOk);
}

From source file:morphy.utils.RegExUtils.java

public static boolean matches(Pattern pattern, String stringToTest) {
    try {/*  w  w  w . jav a2s  . c o m*/
        return pattern.matcher(stringToTest).matches();
    } catch (Throwable t) {
        LOG.warn("matches threw exception. regex=" + pattern.pattern() + " test=" + stringToTest, t);
        return false;
    }
}

From source file:ddf.catalog.content.data.impl.ContentItemValidator.java

private static void validateInput(final String input, Pattern pattern) {
    if (!pattern.matcher(input).matches()) {
        throw new IllegalArgumentException("Illegal characters found while validating [" + input
                + "]. Allowable values must match: " + pattern.pattern());
    }/* w  ww  . j  a v  a2 s .  c o  m*/
}

From source file:TimeFormatUtil.java

/**
 * @param seconds/* ww  w. ja v  a 2  s.  c  o m*/
 * @return a string which represents a floating point number with max. 2
 *         decimals
 */
public static String formatTimeTo2Digits(double seconds) {
    String timeString = Double.toString(seconds);

    int dotPos = timeString.indexOf(".");
    if (dotPos < 0)
        return timeString;

    Pattern p = Pattern.compile("([0-9]{1,})\\.([0-9]{1,})");
    Matcher m = p.matcher(timeString);
    if (!m.matches())
        throw new RuntimeException(
                "WARNING: pattern '" + p.pattern() + "' " + "did not match input '" + timeString + "'");

    StringBuilder b = new StringBuilder(m.group(1));
    b.append('.');
    String afterCommaDigits = m.group(2);

    if (afterCommaDigits.length() > 2)
        b.append(m.group(2).substring(0, 2));
    else
        b.append(afterCommaDigits);

    return b.toString();
}

From source file:jef.tools.string.RegexpUtils.java

private static void addToCache(Pattern p) {
    if (cache.size() == PATTERN_CACHE_SIZE) {
        cache.clear();/*  ww  w .  j  av a  2s  .  c om*/
    }
    cache.put(p.pattern(), p);
}

From source file:org.lockss.util.RegexpUtil.java

public static boolean patEquals(java.util.regex.Pattern p1, java.util.regex.Pattern p2) {
    if (p1 == p2) {
        return true;
    }/*from   w  w w.jav  a  2 s  .co  m*/
    if (p1 == null || p2 == null) {
        return false;
    }
    return p1.flags() == p2.flags() && p1.pattern().equals(p2.pattern());
}