6

I'm using the \((?!\s)([^()]+)(?<!\s)\) regular expression to match (string) but not ( string ) nor () when searching in Sublime Text.

As VS Code doesn't support backreferences in regular expressions, I was wondering how can modify the original regex to get the same result in this editor.

Any ideas?

1 Answer 1

10

You may use

\(([^()\s](?:[^()]*[^()\s])?)\)

See the regex demo

Details

  • \( - a ( char
  • ([^()\s](?:[^()]*[^()\s])?) - Group 1:
    • [^()\s] - a char other than (, ) and a whitespace
    • (?:[^()]*[^()\s])? - an optional sequence (so as to also match strings like (a), with 1 char inside parentheses) of
      • [^()]* - 0+ chars other than ( and )
      • [^()\s] - a char other than (, ) and a whitespace
  • \) - a ) char.

Not the answer you're looking for? Browse other questions tagged or ask your own question.