I have a problem with a series of patterns that have to be compiled. Here is an example:
...(?<NOT>\bNOT\b)|(?<LEFT_PAR>\()|(?<RIGHT_PAR>\))...
These group names "LEFT_PAR" and "RIGHT_PAR" raise an exception:
Exception in thread "main" java.util.regex.PatternSyntaxException:
named capturing group is missing trailing '>'
I'm pretty sure that the underscore is the problem. Searching online I didn't find anything helpful and I can't understand why this _ is causing me this issue when i saw examples in python working great. I've read that it is treated like a normal char in Java Patterns. Obviously, if I delete it, it works great.
Can you help me, please? :) Thanks.
EDIT:
public enum Patterns(){
NOT("\\bNOT\\b"),
LEFT-PAR("\\("),
RIGHT-PAR("\\)"); [...]
}
//i'm cutting enum constructor and toString() method...
//That's the part of the method that uses enum
StringBuilder allPatterns = new StringBuilder();
//This loop creates a unique group of patterns (key-value)
for (validTokens t : validTokens.values())
allPatterns.append("|(?<" + t.name() + ">" + t + ")");
//Compile all Patterns and create a group
Pattern pattern = Pattern.compile(allPatterns.toString().substring(1)); //ERROR
I can't change any of the names because of project's specifics. The method and the enum worked well until now... This _ ruined all xD If you can help me to figure out, please. Thanks.
From the docs:
A capturing group can also be assigned a "name", a named-capturing group, and then be back-referenced later by the "name". Group names are composed of the following characters. The first character must be a letter.
- The uppercase letters 'A' through 'Z' ('\u0041' through '\u005a'),
- The lowercase letters 'a' through 'z' ('\u0061' through '\u007a'),
- The digits '0' through '9' ('\u0030' through '\u0039'),
Underscores are forbidden. Remember that different languages have different regex dialects; what works in Python may not work in Java, and vice versa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With