17 Jun 2011

Search and Replace in Visual Studio

Today I’ve been parsing some text files in Visual Studio 2010 express in a semi-manual kind of way (lots of Find and Replace commands!). They were generally not too bad but I needed to clean them up to bring them into a tool I was writing. Here are some helpful things I worked out along the way:

To remove an empty line, bring up the Find and Replace dialog box (ctrl-h I think) and set the “find what” to

\n\n

and put in the “replace with” box put

\n

That worked fine for my files though you may want to try something a little more robust if you have tabs and whitespace on the line. First click “use” and set the combo box to “regular expressions”. The set “Find what”:

^:b*$\n

and leave “replace with” blank

I also needed to remove brackets along with their contents. Make sure Use Regular Expressions is set, “replace with” is blank and then set “Find words” to:

\(.*\)

Then I needed to remove any line that had an apostrophe in it (don’t ask why!), so as above, but set the “Find words” to:

.*'.*\n

Naturally you can replace the apostrophe in the example above with another character, string, or regex substatement.

Need to get red of any lines that contain any non A-Z characters (ie, uppercase only)?

^.*[^A-Z]+.*$

I also cleared out all the numbers from the file. As above but:

[0-9]*

And finally removing all whitespace is done using the above settings and then in “Find What” goes:

:b

But that last one is in the shortcut list anyway. Regular Expressions (regex) are definitely something worth learning and not just if you’re into programming.

comments powered by Disqus