leemon
leemon

Reputation: 1061

Match strings inside brackets when searching in Visual Studio Code

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?

Upvotes: 7

Views: 8121

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

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.

Upvotes: 11

Related Questions