Regular Expressions
May 22nd, 2008
I’m going to set up this page in hopes that others may find some of them useful. I use these mainly in Eclipse when I want to find/replace code, but there’s nothing that I’m using that should not work just as well in Perl, PHP, ColdFusion, ASP, or any other language that uses Regular Expressions.
In no particular order:
Add “inner” space to parentheses (), brackets [], or curly braces {}
Find: \{(\w[^\}]+)\}
Replace: { $1 }
Result: {myVar} becomes { myVar }
or
Find: \((\w[^\)]+)\)
Replace: ( $1 )
Result: (myVar) becomes ( myVar )
or
Find: \[(\w[^\]]+)\]
Replace: [ $1 ]
Result: [myVar] becomes [ myVar ]
Remove extra whitespace from before curly brace
(remove quotes from post when using)
Find: "\s+\{"
Replace: " {"
Result:
function testing():void
{
becomes
function testing():void {
Add space after a parenthesis
(remove quotes from post when using)
Find: "\((?! |\))" Replace: "( " Result: (((test()))) becomes ( ( ( test())))
Add space before a parenthesis
(remove quotes from post when using)
Find: "(?<! |\()\)" Replace: " )" Result: ( ( ( test()))) becomes ( ( ( test() ) ) )