Home > Net >  Visual Studio Code regex for (not (xx))
Visual Studio Code regex for (not (xx))

Time:09-30

I am trying to have a regex to capture all strings with this shape:

(not  (xx))

I.e. a parenthesis, a "not", two empty spaces, two caracters between parenthesis and a closing parenthesis

I have tried:

(not  (*))

But I get:

Invalid regular expression. Nothing to repeat

enter image description here

Any idea ?

CodePudding user response:

The Nothing to repeat error is due to the * quantifier used to quantify an opening bracket of a capturing group construct.

You need to 1) escape special ( and ) chars, and 2) match any text between the closest ( and ) with a negated character class, here, with [^()]*:

\(not  \([^()]*\)\)

Details:

  • \(not \( - a (not ( string
  • [^()]* - zero or more chars other than ( and )
  • \)\) - a )) string.

If there can be any zero or more whitespace chars between not and (, replace the literal spaces with \s*. If there must be one or more whitespaces, use \s instead:

\(not\s*\([^()]*\)\)
\(not\s \([^()]*\)\)
  • Related