What happened to this regex?

In java, I am trying to define form strings: optional underscores, capital letters, and then braces containing two parameters. That is, things likeMAX{1,2} FUNC{3,7} _POW{9,10}

I decided to defer parameter processing until a later time, so I use a regex:

_?[A-Z]+//{.*//}

But when I try to compile it into a template object, I get the following error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 9
_?[A-Z]+//{.*//}
         ^

Does anyone know what the problem is?

+5
source share
2 answers

You need to avoid curly braces in your expression, otherwise they are treated as a repetition operator. I think you would like to use \for this instead //.

+19
source
. '.*' -. :
Pattern regex = Pattern.compile("_?[A-Z]+\\{[^}]+\\}");

, . , , . : regular-expressions.info - ( !)

+4

All Articles