Example usage for java.util.regex Matcher group

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

Introduction

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

Prototype

public String group() 

Source Link

Document

Returns the input subsequence matched by the previous match.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String regex = "(\\d+?)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "1234";

    Matcher matcher = pattern.matcher(candidate);

    System.out.println(matcher.group());

}

From source file:Main.java

public static void main(String[] args) {
    String regex = "[a-z]@.";
    Pattern p = Pattern.compile(regex);
    String str = "abc@yahoo.com,123@cnn.com,abc@google.com";
    Matcher m = p.matcher(str);

    if (m.find()) {
        String foundStr = m.group();
        System.out.println("Found text is:" + foundStr);
    }/*www. j av  a 2  s . co  m*/
}

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.  ja v a 2s .c o  m
    System.out.println(matcher.group());

}

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\d\\d?\\d?");
    Matcher m = p.matcher("test this 536 in it");
    while (m.find()) {
        System.out.println(m.group()); // print the age in your string
        System.out.println(m.start()); // print the position in the string where it starts
    }//from   w w w  .j ava  2  s .  c o  m
}

From source file:RegExpr4.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("W+");
    Matcher mat = pat.matcher("W WW WWW");

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

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<postPhoneNumber>\\d{4})\\b";
    String source = "1234567890, 12345, and 9876543210";
    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(source);
    while (m.find()) {
        String matchedText = m.group();
        int start1 = m.start("areaCode");
        int start2 = m.start("prefix");
        int start3 = m.start("postPhoneNumber");
        System.out.println("Matched Text:" + matchedText);
        System.out.println("Area code start:" + start1);
        System.out.println("Prefix start:" + start2);
        System.out.println("Line Number start:" + start3);
    }/* www . ja  v a2 s .  c om*/
}

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;/*from   w  w w .j av a  2s. c o m*/
    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);
        }
    }
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("Java");
    String candidateString = "Java Java Java.";
    Matcher matcher = p.matcher(candidateString);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }//w w w.  j  a  v  a  2  s .c  o m
}

From source file:MainClass.java

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

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }/*from   w  w  w .j  ava  2  s.c o m*/
    m1.reset();
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(\\d{3})(\\d{3})(\\d{4})\\b";

    Pattern p = Pattern.compile(regex);
    String source = "1234567890, 12345,  and  9876543210";

    Matcher m = p.matcher(source);

    while (m.find()) {
        System.out.println("Phone: " + m.group() + ", Formatted Phone:  (" + m.group(1) + ") " + m.group(2)
                + "-" + m.group(3));
    }/*from  w  w  w.j a  v a 2 s.c  o  m*/
}