Regular Expressions: Part 3
Courtesy Ondrej Balas, VisualStudioMagazine.com
Regular expressions can be useful in other places, too, such as the find/replace feature of your favorite IDE or text editor, or even in business intelligence (BI).
One often overlooked use of regular expressions is in the Find window of your IDE. Let’s say for example you had Visual Studio open and were tasked with ensuring all private fields in some C# code were prefixed with an underscore. You could find all private fields NOT prefixed by an underscore by searching for:
private (\w+) ([^_]\w*)
This pattern reads as “the word private, followed by a space, followed by one or more word characters, followed by a space, followed by any character that is not an underscore, followed by zero or more word characters.”
To then add the underscore to each of those instances, set the replacement string to:
private $1 _$2
This will replace any matches with the word private, followed by a space, followed by the first capture (the type of variable), followed by a space, then an underscore, and finally the second capture (the original variable name). To see what this looks like in the Visual Studio Find and Replace window, see Figure 1. Don’t forget to make sure you check the box for Use Regular Expressions.
[Click on image for larger view.]
Figure 1. The Visual Studio Find and Replace Window
Another area in which regular expressions can come in handy is in simple BI. One of my clients regularly asks me to pull data from various sources, do some aggregation and put it into a spreadsheet. In doing this, I’ve seen a multitude of data sources: HTML, documents using custom markup languages, social network feeds, and even a zip file full of unrelated text files. And while many tools exist for parsing specific things such as HTML or XML, I usually turn to using regular expressions first, before switching to a more specific tool if necessary.
I generally use a free tool called Expresso, which allows me to quickly build the expression I’ll use for parsing.