Skip to content

Instantly share code, notes, and snippets.

@Fasteroid
Last active September 19, 2024 20:42
Show Gist options
  • Save Fasteroid/94c634f1d6ed16069461016b607bd123 to your computer and use it in GitHub Desktop.
Save Fasteroid/94c634f1d6ed16069461016b607bd123 to your computer and use it in GitHub Desktop.
Substitution regex for adding spaces around braces. Works on regex101 but seems broken in Visual Studio...
(""(?>[^""]|\\"")*?"")|(?!\( )(\()(?!\))(\1??(?>(?>""(?>[^""]|\\"")*?"")|.){4,})(\))(?<! \))
$1$2 $3 $4
For use in Visual Studio (.NET Regex Engine)
FEATURES
- Inserts space after opening and before closing parenthesis if contents between parentheses is longer than <x> (4 in this example)
- Does not do anything to strings
- Ignores parentheses that already have space
- Can be used multiple times to hit nested offenders
BUGS
- Inserts 2 spaces after some strings (could fix this if .NET had conditional replacements)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment