Regular Expressions

Remove white spaces using regular expression [\t ]+

The correct expression to remove white spaces using regular expression is

[\t ]+ — Make sure  you select Regular Expression in Search Criteria

I used the following which id not quite work [\t ]*. The problem with this one is it match 0 or more. So it basically matche every single character rather than matches spaces only. Hope this helps somebody.


It is also a good practice, instead of complete removing white spaces, replace it by a single space as your code might not work if you remove all spaces. The search critera also remove tab characters.

Some variotions

^[\t ]+ =  Will match white spaces that are in the beginning of each line

[\t ]+$ = will match white spaces that are at the end of each line


Copy and Past in Visual Studio and Fix IDs

When you copy a big chunk of ASP.NET code the chances are you are going to run int IDs problem. IDs from the new code is wiped as soon as you past them. Here is a solution to fix it.

Copy the chuck of code into clipboard and past into “Sublime Text 2” for example. Use the following RegPater to call all IDs.

Use the following patter in the replace text

Make sure you have select “Regular Expression” (there is a short cut button for it).
Replace everything it finds. And the copy and past the new code. This should fix the problem.

If your code has control validator, use the following regex as a second run to fix that issues

Please note, do not leave – character in ID as Visual Studio will automatically wipe that out.


Greedy means it will go towards the end of line and will NOT stop at first match if there are other matches available.

'.*'  = Greedy
'.*?' = Lazy

Lazy has extra character (?) in the regex syntax. Similarly you can use ‘+’ operator instead of ‘*’

'.+'  = Greedy
'.+?' = Non Greedy (also called Lazy, Reluctant)

‘+’ mean one or more characters
‘*’ mean zero or more characters

Useful regular expression in Visual Studio

This is for Visual Studio 2012. VS 2012 uses a more standard RegEx rather than earlier version. This may not work in earlier version of VS.

Match images tags

 Regular Expression: <imageurl>.*</imageUrl>


Remove blank lines from Code

Regular Expressoin: ^[space]*\r\n

Reqular Expression in SQL Server Management Studio

Append comma to all integers at start of the lines


Reqular Expresson: ^:z
Replace with : \0,

Make sure you select regular expression when using Find Window.