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

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

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

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

From source file:MainClass.java

public static void main(String[] args) {
    Pattern myRE = Pattern.compile("d.*ian");
    Matcher matcher = myRE.matcher("danian devonian dan");
    matcher.lookingAt();//from w  w w  . jav a 2  s.  c om
    String result = matcher.group(0);
    System.out.println(result);
}

From source file:MainClass.java

public static void main(String[] args) {
    Pattern patt = Pattern.compile("(\\w+)\\s(\\d+)");
    Matcher matcher = patt.matcher("Bananas 123");
    matcher.lookingAt();//from w w  w  .  jav  a2 s .com
    System.out.println("Name: " + matcher.group(1));
    System.out.println("Number: " + matcher.group(2));
}

From source file:GreedyExample.java

public static void main(String args[]) {
    String regex = "(\\w+)(\\d\\d)(\\w+)";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "X99SuperJava";

    Matcher matcher = pattern.matcher(candidate);

    matcher.find();//w  w  w . j ava2s. 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[]) {
    String regex = "(\\w+)(\\d\\d)(\\w+)";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "AAA99SuperJava";

    Matcher matcher = pattern.matcher(candidate);

    matcher.find();/*from  w ww  . j av 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:Main.java

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("<Mark([^>]*)>");

    String myString = "this is a test <Mark this is a test> " + "<i>a</i> <Mark t> <b>x+y</b> <Mark 123>";
    Matcher matcher = pattern.matcher(myString);

    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }/* w  w  w. j a va  2s  .  c o  m*/
}

From source file:Main.java

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

    URL url = new URL("http://www.java.com/");
    URLConnection urlConnection = url.openConnection();
    DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
    String html = "", tmp = "";
    while ((tmp = dis.readUTF()) != null) {
        html += " " + tmp;
    }/*from  w  w  w .  j  a va  2s . c o m*/
    dis.close();

    html = html.replaceAll("\\s+", " ");
    Pattern p = Pattern.compile("<title>(.*?)</title>");
    Matcher m = p.matcher(html);
    while (m.find() == true) {
        System.out.println(m.group(1));
    }
}

From source file:Main.java

public static void main(final String[] args) {
    final String[] lines = new String[] { "1. New", "2. UK", "3. France" };

    final Pattern p = Pattern.compile("\\.\\s+(.*?)\\s+-\\s+(.*)");

    for (final String unparsedText : lines) {
        String newspaper;//w w  w.j a  v a2  s  .  c  o  m
        String country;

        final Matcher m = p.matcher(unparsedText);

        if (m.find()) {
            newspaper = m.group(1);
            country = m.group(2);

            System.out.println("Newspaper: " + newspaper + " Country: " + country);
        }
    }
}

From source file:MatcherGroupParamExample.java

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

    String candidateString = "My name is Bond. James Bond.";

    Matcher matcher = p.matcher(candidateString);

    matcher.find();// www  .  j a  va 2  s .co  m
    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);
    System.out.println(candidateString);

    matcher.find();
    group_0 = matcher.group(0);
    group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
    System.out.println(candidateString);
}

From source file:Test.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("([\\w\\s]+) word");
    Matcher m = p.matcher("Could you test a phrase with some word");
    while (m.find()) {
        System.err.println(m.group(1));
        System.err.println(m.group());
    }/*w  ww  .j  a v a2 s.c  om*/
}