In Java regular expressions, capturing groups can be defined in two ways:
- Using numbers that get incremented automatically (as we discussed earlier).
- Using names.
Starting from Java 7, the regular expressions API offers support for named capturing groups. Named capturing groups are especially useful where there are lots of capturing groups. If we have to insert a new group or remove an existing group, then the numerical order changes for every capturing group that comes after the new or removed group, thus requiring the updating of all those references.
The syntax for defining a capturing group is as follows:
In the preceding line, RE is the pattern we are using for capturing a group.
There are certain rules to ...