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?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Woksin
  • 74
  • 2
  • 9

2 Answers2

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

Brick
  • 3,998
  • 8
  • 27
  • 47
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 (\/\*.*\*\/|\{.*\})