21 November 2008

Visual Studio Search & Replace (Multiple Lines)

After spending some time figuring out how to find and replace multiple line content across multiple files, I figured that VS 08 that I'm using offer this functionality.
This functionality is very powerful due to regular expressions.
Regular expressions seem very confusing and complicated at the first glance, but are very very powerful.

Here is a sample of what I did:
//match this
private void Page_Load\(object sender, System.EventArgs e\)\n.*\{\n:b*if \(!IsAuthenticated\(\)\)\n:b*Server\.Transfer\(GetAppSetting\(GlobalConstants\.NOT_AUTHENTICATED_KEY\)\);

Couple of things worth remembering:
1. \n.* matches zero or more line breaks
2. \ escape any special character as bracket (
3. :b matches space or tab

//replace with this
if (!IsAuthenticated())\n\t\t\t\tServer.Transfer(GetAppSetting(GlobalConstants.NOT_AUTHENTICATED_KEY));\n\n\t\t\tUserRole role = GetUserRole();\n\t\t\tswitch (role)

Hope I'll have more time to play with regular expression since this is extremely powerful concept.

Links:
1. regex library cheat shit :-)
2. msdn regular expression builder

No comments: