Regular Expressions is an extremely useful technology that
I personally find maddeningly difficult to work with at times. Although RegEx
can greatly simplify input validation as compared to other hand-coded methods,
the problem I find is that I only need to use it a few times a year. Because of
this infrequency, every time I have a need for RegEx I find myself going to the
same Web sites or resources, researching the same arcane RegEx syntax. There s
only so much room in my brain; apparently, there s not enough room for RegEx.
Another problem is thatVisual Studio
.NET doesn t provide much RegEx help, but
as is usually the case other developers have the same problems I do and have
helpfully created tools to aid in RegEx development.
Although there are several well-done RegEx tools, the one
I like to use is The Regulator (http://regex.osherove.com) because
it s really a one-stop RegEx shop. The Regulator provides RegEx syntax help,
validates RegEx strings with pattern matching, generates .NET RegEx code in VB
and C#, and compiles the code to a DLL. The Regulator also breaks down and analyzes
your RegEx strings, and even allows you to search the Web from a handy
interface for ready-made RegEx strings that you can use in your code. In addition to being a really useful tool, The Regulator
also has a very clean, VS.NET-like interface (which you can see in Figure 1).
Before finding this tool I had to use my application in debug mode or create a
test harness to try out different RegEx strings. Needless to say, using The
Regulator is far easier and less error-prone. I usually start by creating a new
document, entering into the Input area in the lower-right section one or more
RegEx strings separated by a line feed, and clicking the Match toolbar button.
If any of the input strings pass the RegEx pattern matching, they ll be
displayed in the Match section in the lower middle area. The RegEx Analyzer feature, shown in Figure 2, is very
useful because it breaks the string down into the character-by-character RegEx
instructions and explains what each of them mean. For instance, the Social
Security Number string displayed in Figure 2 ^\d{3}-\d{2}-\d{4}$ is
explained as any digit 3 times followed by a dash and any digit 2 times
followed by another dash and finally any digit 4 times. This is pretty easy to
understand even for me. The ^ and $ characters are explained as the opening
and closing boundaries of the string. Without knowing anything else about
RegEx, you can see how it s actually fairly easy to start becoming productively
familiar with how the syntax works just by trying out strings and using the RegEx
Analyzer feature of The Regulator. In addition to searching for, analyzing, and validating
RegEx strings, The Regulator can also give you a jump start on writing the .NET
code to use these strings with its Generate Code and Compile to Assembly
features. Generate Code allows you to export the few lines of .NET code needed
to include the RegEx string in your own code. As you can see in Figure 3, it
also helpfully includes options for C# and VB.NET versions. The Compile to
Assembly feature is similar to Generate Code, but instead of exporting code, it
compiles the code into a DLL which you can then reference in your project. You probably already have a favorite RegEx tool if you
frequently use RegEx strings in your projects. However, if you ve either never
used RegEx, or, like me, only use it very infrequently, this is the tool that
will make using the RegularExpressionValidator control much easier. Ken McNamee is a
Senior Software Developer with Vertigo Software, Inc., a leading provider of
software development and consulting services on the Microsoft platform. Prior
to this, he led a team of developers in re-architecting the Home Shopping
Network s e-commerce site, http://www.HSN.com,
to 100% ASP.NET with C#. Readers can contact him at [email protected]. Testing Your RegEx Strings
Figure 1: The Regulator is packed
with helpful features for RegEx developers, such as syntax IntelliSense, string
validation and analysis, custom code snippets, code generation, and powerful Web
search capabilities.
Figure 2: The RegEx Analyzer is very
helpful at breaking the RegEx string down into its constituent parts for easier
comprehension. This is especially useful when analyzing a string that you got
from someone else and wish to understand how it works. All About the Code
Figure 3: The Regulator s Generate
Code feature provides either VB or C# code that you can plug in to your own code
to validate strings against the RegEx. If all you need is the assembly, The
Regulator can take the extra step and compile this code to a DLL that you can
reference from your project. Conclusion
The Regulator
An Easy and Useful Regular Expression Workbench
0 comments
Hide comments