Adding space before and after parentheses
I had previously written a regular expression that adds extra space within parentheses, so (test) would become ( test ). However, this gets more complex the more parentheses that are added to the mix. ((test)) and (((test))) require a much more complex regex statement to work correctly (and find the correct ending parenthesis to match up with the correct starting parenthesis).
I was rethinking how I did this and came up with a new method. The only difference is that this test will need to be run twice, once for the opening parenthesis and once for the closing parenthesis. As I usually just do a “Replace All”, I figured it wasn’t that much more difficult than doing it the old way (plus this will work for a much parenthesis nesting as a user puts in).
The Regular Expression is:
Find: \((?! |\)) Replace: "( "
remove the double quotes first…I just wanted to make the space visible.
This regular expression means:
Find an opening parenthesis
\(Then check the next character (but do not include/consume it in the “find”). If it is not a space or a closing parenthesis, match the statement
(?! |\))
This will work on something like:
(((test())))
which, after the first run, becomes
( ( ( test())))
Then the second Regular Expression would be:
Find: (?<! |\()\) Replace: " )"
remove the double quotes first…I just wanted to make the space visible.
This regular expression means (starting from the right this time):
Find a closing parenthesis
\)Then check the previous character (but don’t include/consume it in the “find”). If it is not a space or opening parenthesis, match the statement.
(?<! |\()
This will take the previous finished example of:
( ( ( test())))
and change it to:
( ( ( test() ) ) )
By adding the extra space to my code, I think it makes everything a little bit easier to read. This will work for any type of brackets (or any character for that matter). In the first find statement, you can replace the parenthesis in \) and \( with \[ or \] or whatever you want. The same goes for the second find statement. I don’t know why I didn’t think of doing it this way before instead of trying to figure out how to code a really long match for the opening and closing parentheses. It will definitely make my life easier.
Hi Gareth,
Thanks for the useful articles on regular expressions.
I wonder if you could help with a problem I’m having:
I’m trying to replace ParameterExists(someVar) with IsDefined(“someVar”)
someVar can contain any characters
I thought the Find expression would be ParameterExists\((.)\) and the Replace expression would be IsDefined(\”$1\”)
However the Find expression is not working.
Can you see my error?
Thanks,
Kathy
Hi Kathy,
)
I think I see what the error is…it looks like you forgot the + after the capturing element, so
ParameterExists\(((.)+)\)
However, using that “greedy” capture, it will keep going until it finds the last parenthesis (I wasn’t sure, so I ran a quick test
The way I usually handle this is to do something like this:
ParameterExists\(([^\)]+)\)
This says find ParameterExists( then match all “non-closing parenthesis characters”, then match ). This way it finds the first closing parenthesis it comes across and stops matching, rather than the last.
and for the replace statement, you shouldn’t need to escape the characters, so
IsDefined(“$1″)
should work nicely for you
Hope this helps, and thanks for reading.