It is my understanding that the java.regex package does not have support for named groups (http://www.regular-expressions.info/named.html) so can anyone point me towards a third-party library that does?
I’ve looked at jregex but its last release was in 2002 and it didn’t work for me (admittedly I only tried briefly) under java5.
(Update: August 2011)
As geofflane mentions in his answer, Java 7 now support named groups.
tchrist points out in the comment that the support is limited.
He details the limitations in his great answer "Java Regex Helper"
Java 7 regex named group support was presented back in September 2010 in Oracle’s blog.
In the official release of Java 7, the constructs to support the named capturing group are:
\k<name>to backreference a named group "name"${name}to reference to captured group in Matcher’s replacement stringMatcher.group(String name)to return the captured input subsequence by the given "named group".Other alternatives for pre-Java 7 were:
Gábor Lipták mentions (November 2012) that this project might not be active (with several outstanding bugs), and its GitHub fork could be considered instead.
(Original answer: Jan 2009, with the next two links now broken)
You can not refer to named group, unless you code your own version of Regex…
That is precisely what Gorbush2 did in this thread.
Regex2
(limited implementation, as pointed out again by tchrist, as it looks only for ASCII identifiers. tchrist details the limitation as:
Note: You can find true regex recursion examples in Perl and PCRE regexes, as mentioned in Regexp Power, PCRE specs and Matching Strings with Balanced Parentheses slide)
Example:
String:
RegExp:
Access
Replace
(extract from the implementation)