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 args[]) {
    Pattern pat = Pattern.compile("\\b\\w+@XYZ\\.com\\b");

    Matcher mat = pat.matcher("t@XYZ.com\n" + "a@XYZ.com\n" + "n@XYZ.com");

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

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 www  .  jav a2 s.  com*/

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

From source file:MatcherEndParamExample.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);

    matcher.find();
    int endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[0] + endIndex);

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

    matcher.find();//from   ww  w.  j a va 2 s. co m
    endIndex = matcher.end(0);
    System.out.println(candidateString);
    System.out.println(matchHelper[2] + endIndex);

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

From source file:Main.java

License:asdf

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher("(345+3)*/3asdf234234234/234234234/23asdfaasd1f2a3s4d54fadsf");
    while (m.find())
        System.out.println(m.group());
}

From source file:Main.java

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

    Pattern pattern = Pattern.compile("pattern");
    Matcher matcher = pattern.matcher("infile.txt");

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

From source file:Resetting.java

public static void main(String[] args) throws Exception {
    Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
    while (m.find())
        System.out.println(m.group());
    m.reset("fix the rig with rags");
    while (m.find())
        System.out.println(m.group());
}

From source file:MainClass.java

public static void main(String[] args) {
    Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem);
    while (m.find()) {
        for (int j = 0; j <= m.groupCount(); j++)
            System.out.print("[" + m.group(j) + "]");
        System.out.println();/*from  w ww .  j  a  va  2 s  . c  o  m*/
    }
}

From source file:Groups.java

public static void main(String[] args) {
    Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(poem);
    while (m.find()) {
        for (int j = 0; j <= m.groupCount(); j++)
            System.out.print("[" + m.group(j) + "]");
        System.out.println();/*from  w  ww .  j a va2 s  .c o  m*/
    }

}

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    //  get a matcher object
    Matcher m = p.matcher(INPUT);
    List<String> sequences = new Vector<String>();
    while (m.find()) {
        sequences.add(INPUT.substring(m.start(), m.end()));
    }//from  w w  w .ja  v a2s.  c  o m
}

From source file:Main.java

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

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

    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++) {
            // Get the group's captured text
            String groupStr = matcher.group(i);

            // Get the group's indices
            int groupStart = matcher.start(i);
            int groupEnd = matcher.end(i);

            // groupStr is equivalent to
            inputStr.subSequence(groupStart, groupEnd);
        }/*from w  w w  .j  ava2s  .  c  o m*/
    }
}