0

The following seemingly innocuous pattern is causing a PatternSyntaxException:

String pattern = "^(?:.*)\\s*{$"; // pattern is:   ^(?:.*)\s*{$

As you can see when you look at it in regexpal, it seems to work properly there.

For example, that string should match:

name {

The message I'm getting back is "Illegal repetition near index 9", but I have no idea what I'm doing that's illegal. The offending character is supposed to match any number of whitespace characters between the final section and the {.

Can anyone spot what I'm doing wrong?

2 Answers 2

3

You need to escape the { with \\.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't accept an answer yet for some reason but this has fixed it.
@Wug.. You would be able to do it after 10 minutes.
1

You need to escape your opening curly brace with two backslashes.

String pattern = "^(?:.*)\\s*\\{$"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.