How to Test Regular Expressions in .NET without Giving Yourself Migraines
I write a lot of parse-heavy applications, so naturally I spend a fair amount of my development time writing and testing regular expressions. Regular expressions are one of those programming constructs where you always have a clear idea of what you need to do but you work with them just infrequently enough that you can never actually remember the exact syntax. And if you're like me, this means a multi-hour regex 101 refresher until you get it right.
Rather than testing my regular expressions in the middle of a unit test or in some temporary debug code, I've started using .NET Framework Regular Expressions Demo (scroll down to the bottom) provided by Regular-Expressions.info. It's a simple Windows Forms application that allows you to run quick regex tests using the System.Text.RegularExpressions engine, which is nice because it's actual regular expressions engine you'll be using in your .NET production code.
Let me show you how it works using a couple of examples. Suppose we've written the following regular expressions (in C#) for testing the validity of a user's email address:
string EmailRegex1 = @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"; string EmailRegex2 = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; string EmailRegex3 = @"^[\w-]+@([\w-]+\.)+[\w-]+$";
We need to verify that these regular expressions work in all of our use cases:
Let's see how we can do accomplish this using the .NET Regex Demo:
Plugin your regular expression and your test case into their appropriate fields and hit the match test button.
Whoops! Our test case failed? Perhaps it's because the regex doesn't account for case - I'll re-run the test with the System.Text.RegularExpressions' "Case insensitive" option set to true this time.
That did the trick - so if I wanted to use this particular regex in my production code, I'd just need to remember to set the Regex engine's IgnoreCase option to true:
string EmailRegex1 = @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"; Regex r = new Regex(EmailRegex1, RegexOptions.IgnoreCase); r.Match("[email protected]");
This is way better than testing regular expressions through NUnit tests and debug code.
Download (.zip) http://www.regular-expressions.info/download/csharpregexdemo.zip