.net programming, computers and assorted technology rants

Regular Expressions: Part 3


Courtesy Ondrej Balas, VisualStudioMagazine.com

In Part 1 and Part 2 in this series about regular expressions, I went over some of the key features of regular expressions and how to use them in your code.

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).

Advanced Find/Replace
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

Other Uses

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.

Read More…

http://visualstudiomagazine.com/articles/2014/03/01/regular-expressions-part-3.aspx

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s