Five Habits for Successful Regular Expressions
Subject:   Amen to testing!
Date:   2003-08-22 07:39:48
From:   jimothy
For a project I worked on, we need the ability to parse conditional statements entered by the user (basically, name/value pairs like "orderNumber = "123456" or "quantity >= 12"). Certainly, there are options other than regex to parse that, but regex seemed the simplest solution that fit our needs.

Since this was a Java project, we used the excellent ORO library (now part of the Apache Jakarta project) and JUnit to do our testing. I wrote unit test for both conditions I expected to pass, and those that I expected to fail, as this article suggests. This turned out to be a tremendous time saver (anybody who says unit testing slows development time down needs to take another look at it).

Here's the regex I ended up with:

^[ \t]*([a-zA-Z][\\w]*)[ \t]*(<=|>=|!=|=|>|<|LIKE(?= ))[ \t]*(([\\w]+)|\"(.+)\")

Perhaps some of this articles other suggestions would make that a bit easier to read, but without unit testing, I could never have come up with a successful regex to cover all the possible conditions in a reasonable time.