Named groups

In Java regular expressions, capturing groups can be defined in two ways:

  1. Using numbers that get incremented automatically (as we discussed earlier).
  2. 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:

    (?<name>RE) 

In the preceding line, RE is the pattern we are using for capturing a group.

There are certain rules to specify a named group in Java:

  1. Names are case sensitive, so these are four different named capturing groups:
    • (?<name>RE)
    • (?<Name>RE)
    • (?<NAME>RE)
    • (?<naME>RE)
  1. A name must satisfy the following regular expression:
       [a-zA-Z][a-zA-Z0-9]*

    This means that the name must start with a letter and may contain letters or digits after the first position.

    1. Names cannot be duplicate in Java regex.

    Keep in mind that even when we use a name to define groups, the groups are still numbered as 1, 2, 3, and so on, and that these numbers can also be used in references.

    For example, in the following regular expression, we are specifying four named groups, id, subject, score, and term, all separated by a colon:

        (?<id>d+):(?<subject>[a-zA-Z]+):(?<score>d+):(?<term>[A-Z]+) 
    

    If matched with 123456:Science:97:II, then the following groups will be captured:

    Group "id": "123456" 
    Group "subject": "Science"
    Group "score": "97"
    Group "term": "II"
    Group 1: "123456"
    Group 2: "Science"
    Group 3: "97"
    Group 4: "II"
    ..................Content has been hidden....................

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