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

Group 5

15

Also note that in the case of repeated matches in a capturing group using a quantifier, it will capture the last matched text in the given group.

For example, consider the following regex:

    (w+s+){3} 

If the input text is around the word, then the captured group number one will contain word after the regex execution, even though it also matches around and the before completing the match with the last word.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.3.175