RegEx Buddy

By far the BEST regular expression design and test tool out there. Written by Jan Goyvaerts, co-author of the O’Reilly book, “Regular Expressions Cookbook.” Reasonably priced, liberal license (pay once per developer, use anywhere).

During a requirements meeting, a business analyst hands me a code fragment. Doesn’t understand what it does, but says we need to validate a name using this logic:

#validate account name has no invalid characters
if ($UserName !~ /^([a-zA-Z][\w\_]*)$/) {
die SOAP::Fault
->faultstring("Account $UserName has invalid characters. \n") 

!~ is throwing me off (probably means no match), but the # comment, $ variable and / / used to wrap the regular expression tells me that the script is probably Perl, and the regular expression is this:


Like most developers, I’m a bit spotty on regular expressions–I have to look up references and cheat sheets if I’ve been away from them for a while–but I think the regex is saying the first char must be upper or lower alphabet, then the rest can be word characters or underscores. But I want to be sure that’s what it really is, and I want to make sure the expression will also work in .Net.

So I plug it into RegexBuddy. Note that I’m using Perl, test string “Tes_t”. The yellow highlights show that it’s being matched. I’m also using the Create Tab as training wheels. For example, I clicked on the $, so it the last line with the anchor icon is selected.

So I switch to .Net evaluation, but we get an error this time:

So what do we do? Figure out how to escape an underscore in a .Net regular expression? Not necessarily. If you click on \w, the token row text

Match a single character that is a “word character” (letters, digits, and underscores)

…is highlighted.

So by definition, underscores are covered by “\w”; the “\_” wasn’t even necessary. We should remove it. In order to informally test this change, we can interactively try any number of test combinations in real time, like “simplified_”

Later we realize that the implementation needs to be in client-side JavaScript rather than .Net. So we just switch the regular expression evaluation to JavaScript and re-test. Finally, we also use RegexBuddy to generate the JavaScript code block for us:

This a simple, real-world example of how I’ve used RegexBuddy to first explain and validate a regular expression, and then generate a code block for us in JavaScript.  The step-through debugger features are even more impressive, but there are good examples already out there for that.