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[] arguments) throws Exception {
    String page = loadPage(arguments[0]);
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }/*from  www .ja  va2  s .  co  m*/
}

From source file:Main.java

public static void main(String args[]) {

    // String to be scanned to find the pattern.
    String line = "This order was places.";
    String pattern = "(.*)(\\d+)(.*)";

    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);
    if (m.find()) {
        System.out.println("Found value: " + m.group(0));
        System.out.println("Found value: " + m.group(1));
        System.out.println("Found value: " + m.group(2));
    } else {/*from  w ww .  ja  va  2 s .  co  m*/
        System.out.println("NO MATCH");
    }
}

From source file:Main.java

public static void main(String[] args) {
    String testString = " weight 600 KG AND HEIGHT 84900 CM";
    Pattern pattern = Pattern.compile("(?<=HEIGHT)\\s*([0-9]+)\\s*(?=CM)");
    Matcher matcher = pattern.matcher(testString);
    while (matcher.find()) {
        System.out.println("Height: '" + matcher.group(1) + "'");
    }/*from  w w w.ja  va2 s  .c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CharSequence inputStr = "a\rb";
    inputStr = "a\r\nb";
    inputStr = "a\nb";

    String patternStr = "^(.*)$";
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);
    while (matcher.find()) {
        String lineWithTerminator = matcher.group(0);

        String lineWithoutTerminator = matcher.group(1);
    }/* w  w  w  .  j  a v a 2  s  . c o  m*/
}

From source file:Main.java

License:asdf

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

    String candidate = "asdf Java2s.com";

    Matcher matcher = pattern.matcher(candidate);

    if (matcher.find()) {
        System.out.println("GROUP 0:" + matcher.group(0));
    }//from   w w w  .j a v a  2  s  .c o m

}

From source file:Main.java

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

    if (matcher.find()) {
        System.out.println("GROUP 0:" + matcher.group(0));
        System.out.println("GROUP 1:" + matcher.group(1));
        System.out.println("GROUP 2:" + matcher.group(2));
        System.out.println("GROUP 3:" + matcher.group(3));
    } else {/*from   w  w w .j a  va  2  s  .c  o m*/
        System.out.println("NO MATCHES");
    }
}

From source file:Main.java

public static void main(String[] arguments) throws Exception {
    StringBuffer output = new StringBuffer();

    FileReader file = new FileReader("a.htm");
    BufferedReader buff = new BufferedReader(file);
    boolean eof = false;
    while (!eof) {
        String line = buff.readLine();
        if (line == null)
            eof = true;//from   w ww. j a  v  a 2  s  .c o  m
        else
            output.append(line + "\n");
    }
    buff.close();

    String page = output.toString();
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }
}

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("<row><column>(.*)</column></row>", Pattern.DOTALL);

    Matcher matcher = p.matcher("<row><column>Header\n\n\ntext</column></row>");

    if (matcher.matches()) {
        System.out.println(matcher.group(1));
    }/*  w ww .  j av  a 2 s  .  co m*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);
    // Find group number 0 of the first find
    matcher.find();//from   w w w .j  ava2s .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);
    System.out.println(candidateString);

}

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }/* w w w  .j  av  a 2 s .c o  m*/
    System.out.println(m.appendTail(sb).toString());
}