1

I get the error: "Invalid ecape sequence on this regex:

(\/\*[^/*]*(?:(?!\/\*|\*\/)[/*][^/*]*)*\*\/)|(\{.*?\})

Are there any other regexes that are more suitable or what can I do to fix this regex?

2
  • 3
    escape all the backslashes one more time. Commented Aug 22, 2015 at 15:48
  • Maybe "(?s)/\\*.*?\\*/|\\{.*?}" would be enough for your needs. Commented Aug 22, 2015 at 15:56

2 Answers 2

2

You need to escape the backslashes one more time. This is a "feature" of Java's strings. Java "consumes" the backslashes that you have written because it recognizes special characters like '\t'. When it sees, for example '\/' at the beginning of your regex, it thinks you're asking for a special character, and it complains because this sequence is not valid for that purpose. To get the backslash considered in the regex, you need '\\'.

That being said, this entire approach to handling comments and braces is not going to work generally because it's going to have trouble with a variety of cases like nested blocks in braces. (Just to name one of many.)

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

Comments

0
(\/\*[^\/*]*(?:(?!\/\*|\*\/)[\/*][^\/*]*)*\*\/)|(\{.*?\})

This is the correct regex, you missed escaping the forward slashes which represent the start and end of a regex sequence.

Here is a simplified version (\/\*.*\*\/|\{.*\})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.