Matt Olsen
Matt Olsen

Reputation: 255

invalid regular expression : illegal repetition in Java

I'm practicing regular expressions in java and I have a problem with the line:

Pattern pattern = Pattern.compile("\"(.*?)\": {\"detected\": (.*?), \"version\": (.*?), \"result\": (.*?), \"update\": (.*?)}");

In netbeans I get the error:

invalid regular expression : illegal repetition

How can I fix the regular expression?

Upvotes: 2

Views: 943

Answers (1)

elixenide
elixenide

Reputation: 44831

You need to escape the { and } characters with a backslash (\). To get the literal \, you need a double backslash (\\):

Pattern pattern = Pattern.compile("\"(.*?)\": \\{\"detected\": (.*?), \"version\": (.*?), \"result\": (.*?), \"update\": (.*?)\\}");

Otherwise, it looks like you have a strange (and illegal) repetition expression of the form {m,n}, as in \d{3,5} (3 to 5 digits).

Upvotes: 6

Related Questions