Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include named capture groups in java regex? [duplicate]

Tags:

java

regex

I'm new to regex in Java and I can't figure out how to include named capture groups in an expression. I'm writing a ScrewTurn Image Converter for Confluence's Universal Wiki Converter. This is what I have:

String image = "\\[image(?<align>auto)?\\|\\|{UP\\(((?<namespace>\\w+)\\.)?(?<pagename>[\\w-]+)\\)}(?<filename>[\\w- ]+\\.[\\w]+)\\]";

Pattern imagePattern = Pattern.compile(image, Pattern.CASE_INSENSITIVE);

It's throwing this exception in Pattern.comiple():

java.util.regex.PatternSyntaxException: Unknown look-behind group near index 19
\[image(?<align>auto)?\|\|{UP\(((?<namespace>\w+)\.)?(?<pagename>[\w-]+)\)}(?<filename>[\w- ]+\.[\w]+)\]
                   ^

I've used named capture groups like this before in C# (?<namedgroup>asdf), but not in Java. What am I missing?

like image 743
jrummell Avatar asked Jun 07 '10 14:06

jrummell


2 Answers

Java doesn't support named captures.

Source: Are Java and C# regular expressions compatible?

like image 90
Martin Eve Avatar answered Nov 10 '22 15:11

Martin Eve


It appears Java will only start supporting named capturing groups in version 7: Matcher (SE 6) vs Matcher (SE 7)

Also see: http://java.sun.com/javase/7/docs/api/java/util/regex/Pattern.html#groupname

like image 42
Mark Peters Avatar answered Nov 10 '22 17:11

Mark Peters