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

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

    String candidateString = "Java Java Java.";

    // Attempt to match the candidate String.
    Matcher matcher = p.matcher(candidateString);

    // loop though and display all matches
    while (matcher.find()) {
        System.out.println(matcher.group());
    }/*from  w  ww.j  a  va  2 s. co m*/
}

From source file:MainClass.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();
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

}

From source file:ReaderIter.java

public static void main(String[] args) throws IOException {
    // The RE pattern
    Pattern patt = Pattern.compile("[A-Za-z][a-z]+");
    // A FileReader (see the I/O chapter)
    BufferedReader r = new BufferedReader(new FileReader("ReaderIter.java"));

    // For each line of input, try matching in it.
    String line;/* w  w  w  . ja  v  a 2 s.  c o  m*/
    while ((line = r.readLine()) != null) {
        // For each match in the line, extract and print it.
        Matcher m = patt.matcher(line);
        while (m.find()) {
            // Simplest method:
            // System.out.println(m.group(0));

            // Get the starting position of the text
            int start = m.start(0);
            // Get ending position
            int end = m.end(0);
            // Print whatever matched.
            // Use CharacterIterator.substring(offset, end);
            System.out.println(line.substring(start, end));
        }
    }
}

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/* w w  w  .ja  v  a  2  s  . com*/
        System.out.println("No Match");
}

From source file:MainClass.java

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

    String candidateString = "My name is Bond. James Bond.";
    String matchHelper[] = { "               ^", "              ^", "                           ^",
            "                          ^" };
    Matcher matcher = p.matcher(candidateString);

    // Find the end point of the second 'B(ond)'
    matcher.find();
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + endIndex);

}

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

From source file:Main.java

public static void main(String[] args) {
    String s = "(123, 234; 345, 456) (567, 788; 899, 900)";
    Matcher m = Pattern.compile("\\d+").matcher(s);
    List<Integer> numbers = new ArrayList<Integer>();
    while (m.find()) {
        numbers.add(Integer.parseInt(m.group()));
    }// w w w.j a  v  a2  s. c o  m
    System.out.println(numbers);
}

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);

    // Find the starting point of the first 'test'
    matcher.find();
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(startIndex);

    // Find the starting point of the second 'test'
    matcher.find();/* w ww  .  j  av a2  s .  c  om*/
    int nextIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(nextIndex);
}

From source file:Main.java

public static void main(String[] args) {
    // A group of 3 digits followed by 7 digits.
    String regex = "\\b(\\d{3})\\d{7}\\b";

    // Compile the regular expression
    Pattern p = Pattern.compile(regex);

    String source = "12345678, 12345, and 9876543210";

    // Get the Matcher object
    Matcher m = p.matcher(source);

    // Start matching and display the found area codes
    while (m.find()) {
        String phone = m.group();
        String areaCode = m.group(1);
        System.out.println("Phone: " + phone + ", Area  Code:  " + areaCode);
    }/*from w w w .  jav a2s  . c  om*/
}

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();
    int startIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + startIndex);

    //Find the starting point of the second 'Bond'
    matcher.find();//from  ww  w  .  j a v  a2  s . c  o  m
    int nextIndex = matcher.start();
    System.out.println(candidateString);
    System.out.println(matchHelper[1] + nextIndex);

}