Regular Expression to negate non-matching characters

While constructing a regular expression pattern, sometimes it may be easier to write a pattern that negates the non-matching characters (did I just write a double negative?) rather than write a more complex pattern to pick matching characters. Let's say you want to ignore a word that has vowels.

The following pattern will match any single character NOT in the specified set of characters.
[^aeiou]

There is some nice info on Character Classes on MSDN.

[^character_group] represents a Negative character group. It matches any character not in the specified character group

Now we want the pattern to match not just a single character but a complete word so we use the + to indicate there are multiple characters. The ^ sign represents start of a string and $ denotes end of a string, so our final pattern to ignore a whole word that has vowels becomes this -
^[^aeiou]+$

Regular expressions can drastically replace several lines of validation code. However we need to test our pattern aggressively to avoid among other things, false positives.

Over the years, the RegExLib.com's Regular Expression Cheat Sheet has been a good reference for me.

Also see: Book Review: Teach Yourself Regular Expressions in 10 Minutes

Comments