Group numbering

Capturing groups are numbered in increasing numbers, starting with number one. Java regular expressions support up to 99 capturing groups. Group zero always stands for the entire matched text.

For nested capturing groups, group numbers are incremented with the appearance of the opening parenthesis from left to right.

To understand this better, let's consider the following regular expression with nested multiple capturing groups:


It will match the input string as follows:

    a-c/15     a-d/99     b-c/567     b-d/1000 

For the input string, a-c/15, we will get the following captured groups:

Group Num

Captured Text

Group 0


Group 1


Group 2


Group 3


Group 4


Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.