Regular Expressions in Visual Studio 2010

Regular Expressions is a power tool that is used in Web Application for verifying valid emails address, password strength among other things. If you have not worked in Web Development environment, you are missing on this powerful tool.

Interesting regular expressions search is available in Visual Studio. Although Visual Studio’s regular expression does not comply with the regular expression implemented in other languages like PHP, Ruby etc, they still can be used in very powerful way.

Case studies

Putting “,” at the end of a link

Example you have a scenario where you just copied the integer data as follows

12345
34455
12244

(assume there are a lot of them)

You want to put a , at the end of each line, who do you do it? You can do it using regular expression search and replacement.

Bring in the Search Dialog by Ctrl+F. Screen shot is attached.

Regular Expression Search in Visual Studio

Note that the last check box Use “Regular expression” is checked. Now you can type a regular expression in the find field and can perform your search. As I have said Visual Studio 2010 does not use the same Reg Expression as in JavaScript and other languages, the good thing is, some help is already available. Click on the right arrow icon next to “Find What:” text box. It shows the reg expression option available to you.

In this case choose the $ End of Line. Now when you click find, it will stop at the end of every line.

Next click the “Quick Replace” tab. And enter “,” in the replace kewyord (without the quotes). And click replace.

You may want to hit replace each time you want to insert a comma. Doing replace all will put , at the end of every line which is not desirable. You can do that with a more complex regular expression. But this is just a quick toturial.

This entry was posted in Software Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s