Since Java 7 regular expressions API offers support for named capturing groups.
The method java.util.regex.Matcher.group(String) returns the input subsequence captured by the given named-capturing group, but there's no example available on API documentations.
What is the right syntax to specify and retrieve a named capturing group in Java 7?
解决方案
Specifying named capturing group
Use the following regex with a single capturing group as an example ([Pp]attern).
Below are 4 examples on how to specify a named capturing group for the regex above:
(?[Pp]attern)
(?[Pp]attern)
(?[Pp]attern)
(?[Pp]attern)
Note that the name of the capturing group must strictly matches the following Pattern:
[A-Za-z][A-Za-z0-9]*
The group name is case-sensitive, so you must specify the exact group name when you are referring to them (see below).
Backreference the named capturing group in regex
\k
\k
\k
\k
The named capturing group is still numbered, so in all 4 examples, it can be back-referenced with \1 as per normal.
Refer to named capturing group in replacement string
${Name}
${group1}
${name}
${NAME}
Same as above, in all 4 examples, the content of the capturing group can be referred to with $1 in the replacement string.
Named capturing group in COMMENT mode
Using (?[Pp]attern) as an example for this section.
Oracle's implementation of the COMMENT mode (embedded flag (?x)) parses the following examples to be identical to the regex above:
(?x) ( ? [Pp] attern )
(?x) ( ?< name > [Pp] attern )
(?x) ( ?< n a m e > [Pp] attern )
Except for ?< which must not be separated, it allows arbitrary spacing even in between the name of the capturing group.
Same name for different capturing groups?
While it is possible in .NET, Perl and PCRE to define the same name for different capturing groups, it is currently not supported in Java (Java 8). You can't use the same name for different capturing groups.
Named capturing group related APIs
New methods in Matcher class to support retrieving captured text by group name:
The corresponding method is missing from MatchResult class as of Java 8. There is an on-going Enhancement request JDK-8065554 for this issue.
There is currently no API to get the list of named capturing groups in the regex. We have to jump through extra hoops to get it. Though it is quite useless for most purposes, except for writing a regex tester.