Regex to the rescue

Posts   
 
    
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 14-Oct-2005 15:22:15   

Last week I needed to implement a system wide change to our code-base. This change would require the whole team 2 to 3 days of work and I knew I could not afford that. I thought about Regex (which I always found both facinating but more of a voodo art than science). I got a tool (RegexBuddy) and suddenly Regex was almost SIMPLE. I wrote a simple routine that used a couple of Regex's I build with the help of RB (love the way it integrates into VS) and done the job alone in half a day... Now, I got PowerGrep from the same company (which RegexBuddy integrates easily with) and I can do multi-line search-and-replace I wouldn't even think were possible to automate... I am really starting to appreciate the power of Rgex in other areas as well (Regex validators in ASP.NET to name one) but was wondering what areas I could apply my new found Regex swiss-knife.. any ideas guys ??

arschr
User
Posts: 893
Joined: 14-Dec-2003
# Posted on: 14-Oct-2005 15:51:17   

There once was a programmer who was trying to solve a problem. He decided to use regular expresions to solve it. Now he has two problems. wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 14-Oct-2005 16:21:07   

arschr wrote:

There once was a programmer who was trying to solve a problem. He decided to use regular expresions to solve it. Now he has two problems. wink

LOL smile

I like regexps for the fact that you can do things with them which otherwise would take a full parser. (afterall, it's an NFA lexer simple_smile )

For 1.0.2005.1 for example, the name overwriting for procs has been enhanced, so I needed to centralize the routines. I moved to a regexp to find the elements in the various ways a proc name could be formulated. Check it:


Regex procNamePartFinder = new Regex(@"((?<catalogName>\[[\w\.]+\]|\w+(?=\.)).)?(?<schemaName>\[[\w\.]+\]|\w+).(?<procName>\[[\w\.]+\])", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

Impossible to read, but it works wink .

The syntaxis is absolutely horrible, you need trial/error and testcode to get it working. But once it works, it's great simple_smile

Frans Bouma | Lead developer LLBLGen Pro
netclectic avatar
netclectic
User
Posts: 255
Joined: 28-Jan-2004
# Posted on: 14-Oct-2005 17:14:55   

I agree, Regexps can be very handy. Just don't get carried away or you'll end up in a horrible place!!

I've got a number of handly little classes / methods that i use for regexp routines, my favourite of which is for find / replace. It enables me to easily find [PREFIX]NEEDLE[SUFFIX] and just replace the NEEDLE part, where PREFIX & SUFFIX can be also regex expressions, which means i can match on a range of prefixes and/or suffixes.

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 14-Oct-2005 19:08:35   

Only problem I have with Regular Expressions is that it's a deductive process. The pattern starts shaping up only as you add new input strings. You're always n+1 input string away from perfection. You must keep asking yourself, "What if the string looks like this?"

Jeff...