In java, I'm trying to detect strings of the form: optional underline, capital letters, and then curly brackets encasing two parameters. I.e. things like MAX{1,2}
FUNC{3,7}
_POW{9,10}
I've decided to put off dealing with the parameters until later, so the regex I'm using is:
_?[A-Z]+//{.*//}
But I'm getting the following error when trying to compile it into a Pattern object:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 9
_?[A-Z]+//{.*//}
^
Anyone know what the problem is?
You need to escape the curly brackets in your expression, else they are treated as a repetition operator. I think you'd want to use \
for this instead of //
.
John is correct. But you also don't want to use the '.*'
greedy-dot-star. Here is a better regex:
Pattern regex = Pattern.compile("_?[A-Z]+\\{[^}]+\\}");
Note that you do NOT need to escape the curly brace inside a character class. This is fundamental syntax which you need to learn if you want to use regex effectively. See: regular-expressions.info - (an hour spent here will pay for itself many times over!)
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