Example usage for java.util.regex Matcher matches

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

Introduction

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

Prototype

public boolean matches() 

Source Link

Document

Attempts to match the entire region against the pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    String str = "_6654_1";

    Pattern p = Pattern.compile("_(\\d+)_1");
    Matcher m = p.matcher(str);
    if (m.matches())
        System.out.println(m.group(1)); // prints 6654
}

From source file:Main.java

public static void main(String[] args) {
    String s = "this is a good example with 234 songs";

    Pattern p = Pattern.compile("this is a (.*?) example with (\\d+) songs");
    Matcher m = p.matcher(s);
    if (m.matches()) {
        String kind = m.group(1);
        String nbr = m.group(2);//w  w  w .ja v a2 s  . co m

        System.out.println("kind: " + kind + " nbr: " + nbr);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(\\w+) (\\w+)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "Jack Lee";

    String replacement = "$2, $1";
    Matcher matcher = p.matcher(candidateString);
    matcher.matches();

    matcher.appendReplacement(sb, replacement);

    System.out.println(sb.toString());
}

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();//from  w  ww.  ja  v a  2 s. co m
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    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();/*from  www  .java 2 s.  co  m*/
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    System.out.println("second pattern matches " + isOk);
}

From source file:Main.java

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

    CharSequence inputStr = "abc\ndef";
    String patternStr = ".*c.+d.*";

    Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches(".*c.+d.*", "abc\r\ndef");
    matchFound = pattern.matches("(?s).*c.+d.*", "abc\r\ndef");
}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches("[a-c]+", "aBc");
    matchFound = pattern.matches("(?i)[a-c]+", "aBc");

}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Use an inline modifier
    matchFound = pattern.matches("abc", "aBc");
    matchFound = pattern.matches("(?i)abc", "aBc");
    matchFound = pattern.matches("a(?i)bc", "aBc");
}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches("((?i)a)bc", "aBc");
    matchFound = pattern.matches("(?i:a)bc", "aBc");
    matchFound = pattern.matches("a((?i)b)c", "aBc");
    matchFound = pattern.matches("a(?i:b)c", "aBc");
}

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();//  ww w .j  a  v a  2s. com

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

}