How to include named capture groups in java regex?

Posted by jrummell on Stack Overflow See other posts from Stack Overflow or by jrummell
Published on 2010-06-07T14:12:16Z Indexed on 2010/06/07 14:22 UTC
Read the original article Hit count: 206

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about regex