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
  • 28,596
  • 24
  • 84
  • 120
Juju
  • 70
  • 8

5 Answers5

5

Try escaping curly braces:

String regex = "\\{(.*?)\\}";
Mikita Belahlazau
  • 15,025
  • 1
  • 36
  • 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,415
  • 21
  • 28
1

Escape the {}s:

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

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

theglauber
  • 27,189
  • 7
  • 28
  • 46
0

Not entirely clear what you are trying to do by

\{.*\}

should work

MK.
  • 32,464
  • 18
  • 70
  • 108
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
slackwing
  • 27,451
  • 15
  • 82
  • 136