1

I have a random variable lets call it R inside a curly brackets like so:

{R}

I have tried to regex it with this:

{(.*?)//}

I then have this error

"Caused by: java.util.regex.PatternSyntaxException: 
Syntax error U_REGEX_RULE_SYNTAX near index 1:"

Indicator targeting {(.*?)} "("

I tried doing it without the brackets same error. This time indicator targets "."

Could someone help me find an alternate solution to regex items inside a curly brackets?

Cristik
  • 30,989
  • 25
  • 91
  • 127
Juju
  • 70
  • 8

5 Answers5

5

Try escaping curly braces:

String regex = "\\{(.*?)\\}";
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
3

Curly brackets are used in regexp to define specific sequence repetition.

you have to escape them in the regexp.

\{(.*?)\}

should work better

dweeves
  • 5,525
  • 22
  • 28
1

Escape the {}s:

String regStr = "\\{.\\}";

I've found this interactive regex testing page useful in refining Java regular expressions.

theglauber
  • 28,367
  • 7
  • 29
  • 47
0

Not entirely clear what you are trying to do by

\{.*\}

should work

MK.
  • 33,605
  • 18
  • 74
  • 111
0

You can escape special characters using a backslash \. See What special characters must be escaped in regular expressions? for more information (although there is no general rule). Try escaping the curly braces {} and slashes //.

Community
  • 1
  • 1
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145