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[]) {
    Pattern p = Pattern.compile("test", Pattern.CASE_INSENSITIVE);
    String candidateString = "Test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find(0);//from w  ww. j av a2  s.co  m
    System.out.println(matcher.group());

}

From source file:MatcherGroupCountExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("B(ond)");

    String candidateString = "My name is Bond. James Bond.";
    Matcher matcher = p.matcher(candidateString);

    int numberOfGroups = matcher.groupCount();
    System.out.println("numberOfGroups =" + numberOfGroups);

}

From source file:Main.java

public static void main(String[] args) {
    String patternStr = "b";
    Pattern pattern = Pattern.compile(patternStr);

    CharSequence inputStr = "a b c b";
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();
    System.out.println(matchFound);

    String match = matcher.group();
    System.out.println(match);/*from w ww . j  av a 2 s.com*/

    int start = matcher.start();
    int end = matcher.end();
    System.out.println(start);
    System.out.println(end);
    matchFound = matcher.find();
}

From source file:Main.java

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

    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is a test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();// w ww .  j av a  2  s.  c  om
    String group_0 = matcher.group(0);
    String group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
}

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "a b c a b c";
    String patternStr = "a";
    String replacementStr = "x";

    Pattern pattern = Pattern.compile(patternStr);

    // Replace all occurrences of pattern in input
    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
    System.out.println(output);//from ww w  .j  a v a 2s  .  co  m

}

From source file:Main.java

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

    int numberOfGroups = matcher.groupCount();
    System.out.println("numberOfGroups =" + numberOfGroups);
}

From source file:Main.java

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

    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(nextIndex);

}

From source file:RegExpr.java

public static void main(String args[]) {
    Pattern pat;
    Matcher mat;/*from   w w  w  .jav  a 2 s  . c  om*/
    boolean found;

    pat = Pattern.compile("Java");
    mat = pat.matcher("Java");

    found = mat.matches();

    if (found)
        System.out.println("Matches");
    else
        System.out.println("No Match");

    mat = pat.matcher("Java 2");

    found = mat.matches();

    if (found)
        System.out.println("Matches");
    else
        System.out.println("No Match");
}

From source file:Main.java

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

    matcher.find();// w w  w. j  ava2  s  .c om
    int startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(startIndex);

}

From source file:Main.java

public static void main(String[] args) {
    String str = "This is a 23.4.123 test.";
    String[] s = str.split(" ");
    Pattern p = Pattern.compile("(\\d)+\\.(\\d)+");
    double d;//  w  ww . j a  va  2 s.com
    for (int i = 0; i < s.length; i++) {
        Matcher m = p.matcher(s[i]);
        while (m.find()) {
            d = Double.parseDouble(m.group());
            System.out.println(d);
        }
    }
}