Juju
Juju

Reputation: 70

Regex values inside curly brackets

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?

Upvotes: 1

Views: 1956

Answers (5)

Andrew Cheong
Andrew Cheong

Reputation: 30273

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 //.

Upvotes: 0

theglauber
theglauber

Reputation: 29595

Escape the {}s:

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

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

Upvotes: 1

MK.
MK.

Reputation: 34537

Not entirely clear what you are trying to do by

\{.*\}

should work

Upvotes: 0

dweeves
dweeves

Reputation: 5605

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

you have to escape them in the regexp.

\{(.*?)\}

should work better

Upvotes: 3

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

Try escaping curly braces:

String regex = "\\{(.*?)\\}";

Upvotes: 5

Related Questions