How do you test for equality with Regular Expressions?

One of my weakest areas of development is the use of regular expressions. I know generally how they work and what they are intended for, do but every time I need to write one, I struggle. Regular Expressions are use for pattern matching within strings. You define your pattern (a regular expressions), and check to see if your string matches that pattern.

I've been working on a project, where I want to check if a string is equal to any values from a list. Let's say someone enters a name, and I want to see if they are entering a name that already exists.

If I had the list of names to check against in an Array, I could loop over them and do a check. The psuedo-code would be something like this:

var MyValueIsInList : Boolean = false
var MyValue : String = 'Jeff';
var MyList : Array = new Array('Jeff','Sean','Jennifer','Reboog')
for each (var tempString in MyList) {
if(tempString == MyValue){
MyValueIsInList = true;
break;
}
}

That code was written in a browser and completely untested; but that should be the gist of it. For yesterday's task I could have put my list of static values into an Array and used his approach. My intuition told me that using a Regex would be better.

You can read all about regular expressions in Flex here. Flex has a RegExp class to deal with regular expressions which handles this sort of stuff. The class can use two methods, either test or exec. Test checks for validity and returns a boolean value. exec performs the regular expression on the string and returns an array of passing values. I used test.

But, before I can actually do that, I need a regular expression. From the example above, I have 5 values that need to be compared. I can list all those values using the alternator symbol which is a pipe character. '|'.

var MyList : RegExp= new RegExp /Jeff|Sean|Jennifer|Reboog/

The pipe character, in essence, is an "or" for regular expressions. It means find "Jeff" or "Sean" or "Jennifer" or "Reboog". Unfortunately, patterns are designed for string matching. If we compare this RegEx against the text "Jeffry" it will validate to true, because "Jeff" is included in "Jeffry". To get around this, first we group our options using parenthesis:

var MyList : RegExp= new RegExp /(Jeff|Sean|Jennifer|Reboog)/

Then we add two metacharacters to our expression. In the beginning, we add the carrot symbol '^'. This tells our regular expression that the text must come at the beginning of the string. At the end we add the dollar sign '$'. This tells us that the text must come at the end of the string.

var MyList : RegExp= new RegExp /^(Jeff|Sean|Jennifer|Reboog)$/

With these two extra symbols in place, we can use the expression to test for equality. "Jeff" will pass, but "Jeffry won't. "Jennifer" will pass, but "Jenn" won't. To test, use something like this:

var ComparePass1 : Boolean = MyList .test('Jeff');
var ComparePass2 : Boolean = MyList .test('Jeffry');
var ComparePass3 : Boolean = MyList .test('Jenn');
var ComparePass4 : Boolean = MyList .test('Jennifer');
var ComparePass4 : Boolean = MyList .test('Reboog');

Much thanks to Sean, Doug, and Ryan (via Sean), for helping me out w/ this on Twitter yesterday.

I've been having the worst luck with server problems lately. E-mail was down for four days about 2 weeks ago due to a hardware problem (and no backups). This server (my blog server) didn't go down, but the IP that hosted it vanished. My host switched to an alternate IP so I could get it back up, but that does leave the ctcfug.com site still down as I don't control the DNS for that one. I'm standing in as Manager for tonights meeting (on Model Glue). The announcement went out late due to server problems and there are no RSVPs, possibly also due to the server problems. What fun!

Comments
Gareth Arch's Gravatar If you want to play around with regular expressions, gskinner.com has a really nice flex application that I'm constantly using to try them out.
http://gskinner.com/RegExr/
It lets you dump in a large chunk of text and try out your regular expression, instead of having to recompile (in Flex) or constantly refresh (in CF) to test them out.
# Posted By Gareth Arch | 5/21/08 2:58 PM
All Content Copyright 2005, 2006, 2007 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.8.