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:MatcherStartExample.java

public static void main(String args[]) {
    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "          ^", "                      ^" };
    Pattern p = Pattern.compile("Bond");
    Matcher matcher = p.matcher(candidateString);

    //Find the starting point of the first 'Bond'
    matcher.find();/*from  ww  w .j a v  a 2 s .c om*/
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

    //Find the starting point of the second 'Bond'
    matcher.find();
    int nextIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

}

From source file:Main.java

public static void main(String[] args) { // main method
    Pattern pattern = Pattern.compile("(?<=sentence).*");
    Matcher matcher = pattern.matcher("this is a test!");
    boolean found = false;
    while (matcher.find()) { // if it is found
        System.out.println("I found the text: " + matcher.group().toString());
        found = true;//from   w ww .  j a  v  a  2s  .com
    }
    if (!found) { // if not
        System.out.println("I didn't find the text."); // say it wasn't found
    }
}

From source file:MainClass.java

public static void main(String[] av) {
    String regEx = "[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)";
    String str = "a b c d e 1 2 3 4.5 ";
    Pattern pattern = Pattern.compile(regEx);
    Matcher m = pattern.matcher(str);
    while (m.find()) {
        for (int i = 0; i <= m.groupCount(); i++) {
            System.out.println("Group " + i + ": " + m.group(i)); // Group i substring
        }//w w  w  .jav  a2 s .  c om
    }
}

From source file:Main.java

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

    String inputStr = "abbabcd";
    String patternStr = "(a(?:b*))+(c*)";
    // (?:b*) is a non-capturing group

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        for (int i = 0; i <= matcher.groupCount(); i++) {
            String groupStr = matcher.group(i);
        }/*from   w  w  w  . j  av  a2  s  .co m*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String filename = "infile.txt";
    String patternStr = "pattern";
    BufferedReader rd = new BufferedReader(new FileReader(filename));

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher("\\D");

    String line = null;/*  ww w.j a v  a2 s.  c  o m*/
    while ((line = rd.readLine()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            // line matches the pattern
        }
    }
}

From source file:Main.java

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

    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";

    // Compile and use regular expression
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        // Get all groups for this match
        for (int i = 0; i <= matcher.groupCount(); i++) {
            String groupStr = matcher.group(i);
        }/*ww w  .ja  v a 2  s.  co  m*/
    }
}

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();/*from   w w  w . jav a 2 s . c om*/

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

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create matcher on file
    Pattern pattern = Pattern.compile("pattern");
    Matcher matcher = pattern.matcher(fromFile("infile.txt"));

    // Find all matches
    while (matcher.find()) {
        // Get the matching string
        String match = matcher.group();
    }//from  w  ww  .j  a v  a2  s .  c o m
}

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:NonCapturingGroupExample.java

public static void main(String args[]) {
    String regex = "hello|hi|greetings|(?:good morning)";

    String candidate1 = "Java2s say hi to you";
    String candidate2 = "Java2s say good morning to you";
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(candidate1);
    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 1:" + candidate1);

    matcher.reset();//w w  w.jav  a2  s. c  o  m
    matcher = pattern.matcher(candidate2);

    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 2:" + candidate2);
}