Just as a pattern can have groups, so can it have subgroups. Subgroups are simply smaller groups within the larger whole. They're separated from the original group, and from each other, by being surrounded by parentheses.
In the example in the preceding section, in order to be able to refer explicitly to the digit \d, you modify the pattern to \w(\d). Here, \w\d is group(0) and (\d) is group(1). Listing 3-2 demonstrates the use of a subgroup to extract the part of the candidate that matches the digit \d.
![]() |
import java.util.regex.*; public class SimpleSubGroupExample{ 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 the parts that //match. Matcher matcher = p.matcher(candidate); if (matcher.find()){ //Extract 'A9', which matches group(0), which is //always the entire pattern itself. String tmp = matcher.group(0); System.out.println(tmp); //tmp is 49 //extract part of the candidate string that matches //group(1): Namely, the '9' which follows the 'A' tmp = matcher.group(1); //tmp is 9 System.out.println(tmp); } } }
![]() |
Listing 3-2 allows you to extract parts of the candidate String that match the entire expression. It also allows you to extract subsections of that matching section. Thus, you can extract 9 from the matching region A9 because 9 matches group(1). That is, the regex engine stores the 9 when it matches A9, because you defined the subgroup (\d).