Wednesday, June 15, 2011

Search, Match, Replace, Generate

{

Many moons ago I got about 0.25 seconds of fame when nRegex, a tool I wrote for evaluating regular expressions of the .NET flavor,  got a bit of acclaim. It was one of the better days of my life, an encouragement that sometimes struggling alone in a South Dakota basement can lead to a little bit of notice.

One of the tricks that keeps me going back to nRegex is being able to generate code by using a regular expressions. Regular Expressions, though often reviled, turn out to be quite handy in a lot of situations.

Let’s say you have a table that looks like this:

	CREATE TABLE PackingList(
PackingListId INT IDENTITY PRIMARY KEY,
PackingItem VARCHAR(50),
Destination VARCHAR(50)
)
GO


 



Let’s say you have a list of values:



Shoes
Camera
Laptop
Wallet


The list is short for brevity but let's say you want to insert them all into your table for a destination called //Build/. You know the syntax for an INSERT but it's a bit of a nuisance to type over and over again. One thing you can do is to use a regular expression to match and then reference the results of your match in a replace. Over on nRegex, we'd paste in our list of items into the main text area and then use the regular expression (.+) to match each item, line by line. Because the items are matched into a group with your parenthesis, you can now write something like the following for your replace:



INSERT INTO PackingList VALUES('$1', '//Build/')


And voila! You now just have to copy the results:



INSERT INTO PackingList VALUES('Shoes', '//Build/')
INSERT INTO PackingList VALUES('Camera', '//Build/')
INSERT INTO PackingList VALUES('Laptop', '//Build/')
INSERT INTO PackingList VALUES('Wallet', '//Build/')



But that's just a tip of the iceberg. There's a lot more nifty regular expressions tricks for working with code. Here's another one I run into quite often. Let's say I have some code that looks like this:



  rs["foo"] = myFoo;
rs["bar"] = myBar;


I want to swap what's on either side of the equals sign to do the opposite type of assignment. Here's my regular expression:



(.+)\s=\s(.+);


And my replacement expression



$2 = $1;


Et voila aussi! You can now copy the swapped values to wherever they need to go.




One final thing: any place that offers you regular expressions you can use these techniques. There are some subtle differences but as long as you have a conceptual understanding of what your goal is it's quite easy to bend to the flavor of regular expressions presented. For example, if you are using Notepad++ and want to accomplish the same thing, you reference your groups with a leading backslash rather than the "$" character - in our first example you would use the following:



INSERT INTO PackingList VALUES('\1', '//Build/')


This is, of course, just scratching the surface. It’s not that hard though! Once you learn the meaning of things like ^ or $ then you can manipulate strings in all sorts of ways that may have once not seemed possible. The best way to learn regular expressions hasn’t changed for many years. The two canonical books I always refer to are Mastering Regular Expressions by Jeffrey Friedl and the Regular Expressions Cookbook by Goyvaerts and Levithan.











 



Although nRegex will allow you to hobble by I also recommend RegexBuddy. Rexv (which inspired nRegex) is a good tool though the Regular Expression engine is not .NET.



Last thing: do you have any nifty regex to code generation tricks you use on a regular basis? The audience of one you have in this space would love to learn them.



}

No comments: