Regular Expressions is a great way to find what you're looking for. Unfortunately, it can be also be quite daunting for a beginner who's unsure where to start. Hence I started this post for tips and tricks of using Regular Expressions with Find & Replace in VS.
To start off with, we actually use our own regular expressions engine. If you've never touched regular expressions or wild cards, you may wonder what that “right arrow” button next to the Find what: combo box is for. When the “Use:” option is selected, this button becomes enabled and provides a quick list of the supported syntax commands, depending on whether “Regular Expressions” or “Wildcards” is selected. At the bottom of that list there is also a link to a help topic with the completely supported.
Let's start off with something simple. In this mini-tutorial, I will use the following line on which to perform a simple Find, always from the beginning of the line:
Ros3s ar3 r3d. Vi0l3ts ar3 blu3.
First, some simple Regular Expression syntax:
. - Matches any character except line break (includes other whitespaces)
* - Matches zero or more occurences of the preceding expression
+ - Matches one or more occurances of the preceding expression
 - Matches any one of the characters in . To specify a range, use -
Now, let's get started!
A search with “R.+” searches for a string that begins with R, and is followed by at least one character. Therefore, the first find will highlight the entire line as the result. This is because we allowed for at least one occurance of any character (including whitespace) following a R.
We can exclude line breaks by specifing a specific range of characters. For example, the search “R[a-z3]+“ states a string that begins with R and is followed by at least one character from the range a-z and 3. The first match will be “Ros3s“, but the next match will be “r3“ of the second word “ar3“.
What if we only wanted words that began with R? We can use the following syntax:
< - Beginning of a word
> - End of a word
So a search for “<R[a-z3]“ searches for a word that begins with R and is followed by at least one character from the range a-z and 3. It will return matches for “Ros3s“ and “r3d“. If you just wanted to search for Ros3s, you can narrow down the scope of the search with “<Ro[a-z3]“, which searchs for a word that begins with ro and is followed by at least one character from the range a-z and 3.
Hopefully, this post will help some newbies out there to start experimenting with regular expressions. I will continue to post other tips later on, and also include some regular expressions that I have found really useful.