Example usage for java.util.regex Pattern matches

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

Introduction

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

Prototype

public static boolean matches(String regex, CharSequence input) 

Source Link

Document

Compiles the given regular expression and attempts to match the given input against it.

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(); // 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:PatternConvenience.java

public static void main(String[] argv) {

    String pattern = ".*Q[^u]\\d+\\..*";
    String line = "Order QT300. Now!";
    if (Pattern.matches(pattern, line)) {
        System.out.println(line + " matches \"" + pattern + "\"");
    } else {//w w  w  .j av  a  2s.  com
        System.out.println("NO MATCH");
    }
}

From source file:Main.java

public static boolean isYoyoID(String yoyoID) {
    return Pattern.matches("\\d{1,10}$", yoyoID);
}

From source file:Main.java

public static boolean isPhoneNumber(String s) {
    return Pattern.matches("1[3-9]\\d{9}", s);
}

From source file:Main.java

public static boolean onlyNumberId(String id) {
    if (Pattern.matches("^[0-9]+$", id))
        return false;
    return true;/*from  w  w  w .j a  v  a  2s. co m*/
}

From source file:Main.java

public static boolean isCellPhone(String phone) {
    if (Pattern.matches("\\d{11}", phone == null ? "" : phone)) {
        return true;
    }/*from   www  .j a  va  2  s.co m*/
    return false;
}

From source file:Main.java

public static boolean isValidRizNO(String id) {
    return Pattern.matches("[a-zA-Z0-9]{13,20}$", id);
}

From source file:Main.java

public static boolean checkIsAllDigit(String name) {
    return Pattern.matches("^[0-9]*$", name);
}