FlexFusion

April 17, 2008

Regular Expressions

Filed under: Uncategorized — Gareth @ 2:07 pm

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() ) ) )

1 Comment »

  1. [...] expressions I’m writing (and so others can use them if they wish), I’ve created a Regular Expressions page that I’ll periodically be updating with new regular expressions each time I use them.  I [...]

    Pingback by FlexFusion » Regular Expressions are fun! — April 17, 2008 @ 2:10 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress