As I explained in Chapter 2, a group is simply a sequence of characters that describes a regex pattern. Thus, \w\d is a group, because there are two characters, \w and \d, and they're in a sequence. This is an implicit group and thus trivial, because most groups, as such, are explicitly surrounded by parentheses. In Java regular expressions, every pattern has groups, and they're indexed. Thus, the pattern \w\d has a single group, namely itself, and the index of that group is, of course, 0.
Groups are described in the Pattern but realized in the Matcher. Conceptually, this is similar to how SQL works, where queries are described in the SQL query, but the matching parts are extracted from the ResultSet. Thus, when you describe the pattern \w\d, you might extract the matching candidate A9 from the candidate A9 is my favorite. For example, if the group is described as
Pattern p = Pattern.compile("\\w\\d");
and the candidate String is
String candidate = "A9 is my favorite";
you define a Matcher for this candidate String:
Matcher matcher = p.matcher(candidate);
Assuming that the Matcher.find() method has already been called, then calling Matcher.group(0) returns the part of the candidate String that matches the entire pattern, as follows:
String tmp = matcher.group(0);
Thus, the Matcher.group(0) method is a bit of a misnomer. It doesn't actually extract the group; it extracts the part of the candidate String that matches that group. This is a subtle but important difference. The full example follows in Listing 3-1.
![]() |
import java.util.regex.*; public class SimpleGroupExample{ public static void main(String args[]){ //the original pattern is always group 0 Pattern p = Pattern.compile("\\w\\d"); String candidate = "A9 is my favorite"; //if there is a match, extract that part of //the candidate string that matches group(0) Matcher matcher = p.matcher(candidate); //OUTPUT is 'A9' if (matcher.find()){ String tmp = matcher.group(0); System.out.println(tmp); } } }
![]() |
That works perfectly when you need the whole pattern. But what about cases in which you need subsections of that pattern? How do you extract those? That's where subgroups come to the rescue.