0

I am not able to understand the output of groups, how it is considering each paranthesis in the pattern and equating that with the variable 'line' here. Please explain

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTut3 {

  public static void main(String args[]) {
    String line = "This order was placed for QT3000! OK?"; 
    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 {
      System.out.println("NO MATCH");
    }
  }
}
Madhuri
  • 73
  • 1
  • 1
  • 7
  • Quoting myself... [here](http://stackoverflow.com/questions/17969436/java-regex-capturing-groups/17969620#17969620). – Mena Aug 18 '16 at 14:49
  • The regex just grabs as many chars other than a newline up to the last digit (and placed into Group 1), then the last digit is placed into Group 2, and the rest of the line is placed into Group 3. Read more about greedy quantifiers and backtracking. – Wiktor Stribiżew Aug 18 '16 at 14:50

2 Answers2

-1

The pattern "(.*)(\\d+)(.*)" means:

(.*) - Consume anything and store it in group 0
(\\d+) - Consume a number and store it in the next group, i.e. group 2
(.*) - Consume anything and store it in the next group

In this way the pattern will find a number in the string and store everything before it in group 0, the number in group 1 and the rest of the string in group 2.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
-1

m.group(0) always matches the whole pattern. Your matches actually start at 1, not 0.

m.group(0): matches the whole pattern
m.group(1):  first parenthesis
m.group(2):  second set of parenthesis
m.group(3): third set

Edit: made correction

Ricardo
  • 124
  • 5