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(String name) 

Source Link

Document

Returns the input subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:Main.java

public static void main(String[] args) {
    String str = "_6654_1";

    Pattern p = Pattern.compile("_(\\d+)_1");
    Matcher m = p.matcher(str);
    if (m.matches())
        System.out.println(m.group(1)); // prints 6654
}

From source file:Main.java

public static void main(String[] args) {
    String s = new String("\"Hello\" hello");
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }//from ww w.j  a  v  a2 s . co  m
}

From source file:Main.java

public static void main(String[] args) {
    String s = "this is a good example with 234 songs";

    Pattern p = Pattern.compile("this is a (.*?) example with (\\d+) songs");
    Matcher m = p.matcher(s);
    if (m.matches()) {
        String kind = m.group(1);
        String nbr = m.group(2);/*  www  .j av  a  2 s.c  om*/

        System.out.println("kind: " + kind + " nbr: " + nbr);
    }
}

From source file:Main.java

public static void main(String[] args) {
    String s = "bla bla {a.b.c} bla bla bla {0,1,2} bla bla";
    Pattern p = Pattern.compile("\\{([^\\{\\}]*)\\}");

    Matcher m = p.matcher(s);

    while (m.find()) {
        System.out.println(m.group(1));
    }//from   w w  w.j  a v a2 s.c  o  m
}

From source file:Main.java

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

    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is a test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from  w w w.  j a  va  2  s  .c  om
    String group_0 = matcher.group(0);
    String group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w\\d");
    String candidate = "A6 is my favorite";

    Matcher matcher = p.matcher(candidate);

    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/*from  www  . ja v a2 s .com*/
    }
}

From source file:SimpleGroupExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w\\d");
    String candidate = "J2 is my favorite";

    Matcher matcher = p.matcher(candidate);

    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/*  ww  w  .j  av  a 2  s  .  c  om*/
    }
}

From source file:Main.java

License:asdf

public static void main(String args[]) {
    String regex = "(\\w+)(\\d\\d)(\\w+)";
    Pattern pattern = Pattern.compile(regex);
    String candidate = "X99 asdf 44";

    Matcher matcher = pattern.matcher(candidate);
    matcher.find();// w  ww . j a v  a 2  s.  c  o  m
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\w(\\d)");
    String candidate = "A6 is Audi";

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/* ww w .ja  va2s  .  com*/

        tmp = matcher.group(1);
        System.out.println(tmp);
    }
}

From source file:Main.java

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

    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
        String tmp = matcher.group(0);
        System.out.println(tmp);/*from w  ww .j a  v  a 2  s  . c  o  m*/

        tmp = matcher.group(1);
        System.out.println(tmp);
    }
}