Example usage for java.util.regex Matcher find

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

Introduction

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

Prototype

public boolean find() 

Source Link

Document

Attempts to find the next subsequence of the input sequence that matches the pattern.

Usage

From source file:Main.java

public static void main(String[] arguments) throws Exception {
    StringBuffer output = new StringBuffer();

    FileReader file = new FileReader("a.htm");
    BufferedReader buff = new BufferedReader(file);
    boolean eof = false;
    while (!eof) {
        String line = buff.readLine();
        if (line == null)
            eof = true;//from w  ww .ja  v a  2s.c o m
        else
            output.append(line + "\n");
    }
    buff.close();

    String page = output.toString();
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }
}

From source file:Main.java

public static void main(String[] args) {
    String hello = "HelloxxxHelloxxxHello"; // String you want to 'examine'
    Pattern pattern = Pattern.compile("Hello"); // Pattern string you want to be
                                                // matched
    Matcher matcher = pattern.matcher(hello);

    int count = 0;
    while (matcher.find()) {
        count++; // count any matched pattern
    }//from w w w  .j  av  a  2s.co  m
    System.out.println(count);
}

From source file:MainClass.java

public static void main(String args[]) {
    // create a Pattern
    Pattern p = Pattern.compile("Bond");

    // create a Matcher and use the Matcher.group() method
    String candidateString = "My name is Bond. James Bond.";
    Matcher matcher = p.matcher(candidateString);
    // extract the group
    matcher.find();
    System.out.println(matcher.group());
}

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  a  v a 2 s  .c  o  m
    }
}

From source file:REmatch.java

public static void main(String[] argv) {

    String patt = "Q[^u]\\d+\\.";
    Pattern r = Pattern.compile(patt);
    String line = "Order QT300. Now!";
    Matcher m = r.matcher(line);
    if (m.find()) {
        System.out.println(patt + " matches \"" + m.group(0) + "\" in \"" + line + "\"");
    } else {/*from   w  w w .  ja  v a  2 s  .  c  om*/
        System.out.println("NO MATCH");
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("java");
    Matcher mat = pat.matcher("www.java2s.com 1 2 3 j a v a 2 s.c o m");

    while (mat.find()) {
        System.out.println("java found at index " + mat.start());
    }/*  w  w  w  .  j a  va 2s.c o m*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("Java \\d");

    String candidate = "Java 4";
    Matcher m = p.matcher(candidate);

    if (m != null)
        System.out.println(m.find());
}

From source file:MatcherStartParamExample.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "          ^", "           ^", "                      ^",
            "                       ^" };
    Matcher matcher = p.matcher(candidateString);
    matcher.find();
    int startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

    matcher.find();/*from  ww w.ja v  a  2s .  c o m*/
    startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + startIndex);

    nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(matchHelper[3] + nextIndex);

}

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

From source file:MainClass.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };

    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");

    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        while (m1.find())
            System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end());
    }//from www.  j a  v  a  2  s . co m
}