PatternSyntaxException: Illegal Repetition when using regex in Java
Solution 1:
The {
and }
are special in Java's regex dialect (and most other dialects for that matter): they are the opening and closing tokens for the repetition quantifier {n,m}
where n
and m
are integers. Hence the error message: "Illegal repetition".
You should escape them: "\\{\"user_id\" : [0-9]*\\}"
.
And since you seem to be trying to parse JSON, I suggest you have a look at Jackson.
Solution 2:
There should be plus operator:
user_id : [0-9]+
Double apostrophes only when the string has to contain it.
When the string including curly brackets use:
\{user_id : [0-9]+\}