O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

    (((a|b)-(c|d))/(\d+)) 

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

a-c/15

Group 1

a-c/15

Group 2

a-c

Group 3

a

Group 4

c

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required