5

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Moshe
  • 205
  • 1
  • 6
  • 8

2 Answers2

19

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 Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    ...and because you're writing it in the form of a Java string literal, you have to escape the backslash: `"_?[A-Z]+\\{.*\\}"` – Alan Moore Apr 02 '11 at 18:02
  • @Alan: Unless you use a "/"->"\" front-end converter to save your sanity. :) – tchrist Apr 02 '11 at 18:48
4

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!)

ridgerunner
  • 33,777
  • 5
  • 57
  • 69
  • Small suggestion. To avoid greedy-dot-star, instead of [^}], "Reluctant quantifier (?)" can be used. e.g. ("_?[A-Z]+\\{.*?\\}"). More info at http://stackoverflow.com/questions/1139171/when-it-comes-to-regex-what-is-the-difference-between-greedy-and-reluctant – siddagrl Mar 22 '12 at 10:51
  • @siddagrl - Thanks for the suggestion, but no, `[^}]+` is much more efficient (i.e is faster) than `.*?` and is more accurate as well. To understand why, I highly recommend reading: [Mastering Regular Expressions (3rd Edition)](http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124 "By Jeffrey Friedl. Best book on Regex - ever!"). Cheers! – ridgerunner Mar 22 '12 at 16:07
  • Oh! I did not know that. Thanks for sharing. Will have a look. – siddagrl Jan 02 '13 at 03:59