pf » Regular Expressions - Named Capture Groups
Regular Expressions - Named Capture Groups

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
- Cheat Sheet Roundup - Over 30 Cheatsheets for developers - September 1, 2005
Marius
Jon
- Dear SQL Server Enterprise Manager Developer
- PostalMethods - Web Service for Snail Mail
- Mastering CFQUERYPARAM
- Google Code Search for ColdFusion
- Speaking at CFUNITED 2008
- Getting ColdFusion SQL Statements from SQL Server Trace
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in










