Example usage for java.util.regex Pattern matcher

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

Introduction

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

Prototype

public Matcher matcher(CharSequence input) 

Source Link

Document

Creates a matcher that will match the given input against this pattern.

Usage

From source file:Main.java

public static void main(String args[]) {
    String candidateString = "This is a test. This is another test.";
    Pattern p = Pattern.compile("test");
    Matcher matcher = p.matcher(candidateString);

    matcher.find();//from w  w w . j av a  2s .c om
    int endIndex = matcher.end();
    System.out.println(candidateString);
    System.out.println(endIndex);

}

From source file:MainClass.java

public static void main(String args[]) {
    String output = "";
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }/*  w w  w  . j a  v  a2 s.co m*/
    m1.reset("56789");
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:Test.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("([\\w\\s]+) word");
    Matcher m = p.matcher("Could you test a phrase with some word");
    while (m.find()) {
        System.err.println(m.group(1));
        System.err.println(m.group());
    }//from w  w  w.  j a  va 2  s .  com
}

From source file:Main.java

public static void main(String[] args) {
    String word = "77933211";
    Pattern pattern = Pattern.compile("(\\d)\\1");
    Matcher matcher = pattern.matcher(word);
    word = matcher.replaceAll("$1");
    System.out.println(word);// ww  w.j a  va  2 s.  c  o  m
}

From source file:MainClass.java

public static void main(String args[]) {
    // Match lowercase words.
    Pattern pat = Pattern.compile("[a-z]+");
    Matcher mat = pat.matcher("www.java2s.com.");

    while (mat.find())
        System.out.println("Match: " + mat.group());
}

From source file:Main.java

public static void main(String[] asd) {
    String sourcestring = "source string to match with pattern";
    Pattern re = Pattern.compile("[^ABC\\s+]");
    Matcher m = re.matcher(sourcestring);
    int mIdx = 0;
    while (m.find()) {
        for (int groupIdx = 0; groupIdx < m.groupCount() + 1; groupIdx++) {
            System.out.println("[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
        }//from w w  w  .  java 2  s.c  om
        mIdx++;
    }
}

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();//w w  w. ja  va 2  s.  c om
    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 {
    Pattern punct = Pattern.compile("\\G\\p{Punct}");

    Matcher mat = punct.matcher("this is a test 999");
    System.out.println(mat.find());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String input = "123 321 443 52134 432";
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(input);
    int i = 0;//from ww  w .j a v a 2s .c om
    while (matcher.find()) {
        ++i;
    }
    System.out.printf("Matched %d times%n", i);
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("Java");
    Matcher mat = pat.matcher("Java 2");

    System.out.println("Looking for Java in Java 2.");

    if (mat.find())
        System.out.println("subsequence found");
    else//from  w  ww  .  jav  a 2s.c  o  m
        System.out.println("No Match");
}