Regular Expressions - Named Capture Groups

javamisc

I am porting some code from .NET to Java today, and found out about a very cool feature in regular expressions called Named Groups. Most regular expression implementations let you group text (which can then be used for back-references) using parenthesis, for example: my name is (pete). You then refer to the groups with numbers such as \1 or $1 to match the first group.

Python was the first language to introduce the ability to name groups in regular expressions, PHP along with .NET also supports them. For example my name is (?<fisrtname>pete) although it looks more complicated you can reference the group by name rather than by number. This is really handy in the code I'm porting because there are tons of regular expressions and the ordering of the groups may be different based on the pattern.

However it doesn't look like Java supports this feature, which is a bummer because we are going to have to implement it ourself by preparsing the pattern, and storing the order of the named groups.

It would be really cool if Java 1.6 supported this... Though I don't see any JSR's on the topic.



Related Entries

4 people found this page useful, what do you think?

Trackbacks

Trackback Address: 281/C740A4B5DA9A64A4B000D0BD1945E0DE

Comments

On 03/21/2005 at 4:49:33 PM EST Anthony wrote:
1
According to JDK javadoc, it does support back reference in forms of \1

On 03/21/2005 at 5:31:32 PM EST Pete Freitag wrote:
2
Yes Anthony, I am aware of \1, what I am talking about is named backreferences.

On 03/22/2005 at 3:00:52 PM EST Marius Scurtescu wrote:
3
It looks like JRegex does support named groups: http://jregex.sourceforge.net/

Marius

On 03/22/2005 at 5:53:18 PM EST Keith Lea wrote:
4
JSR's aren't the only (or even preferred) way to make changes like this. I've filed an RFE to Sun for this functionality.

On 01/18/2006 at 4:50:29 PM EST Jon wrote:
5
Java does support it in the built in regular expressions and has for many years since JDK 1.4. Just use $1 as the backrefernce, not \1. The javadoc documentation isn't all that clear about it.

Jon

On 01/18/2006 at 4:52:34 PM EST Jon wrote:
6
Sorry, I took your reply about \1 to mean you didn't understand that Java uses $1. As for named groups, that's another matter.

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?