Matthew Jones

Regular expressions are quite possibly the least enjoyable thing about programming, mostly because I can't read them they're terrible. They're supposed to be used to search for and match within text, but the more often I encounter them, the more often I lament their very existence. Like this one:

What the hell does this match? I've found regular expressions like this before in my day-to-day work, and there are only two possible outcomes from any such encounter:

I figure out what this regex is supposed to be doing, delete it, and replace it with readable code OR

I don't figure out what it is doing and have to leave it for fear of breaking something.

I hate regular expressions. I'll find one in my code, and Scenario #2 inevitably rears its ugly head, and then I'm all:

To me, regular expressions are like meetings: sometimes we need them to do effective work, but damn do they suck. Which is why stumbling on libraries such as VerbalExpressions makes me feel all giddy inside, like rainbows and lollipops will spew forth from my mouth in a torrent of glee.

That happens to everyone, right? Not just me?

What Is VerbalExpressions?

VerbalExpressions is a library that builds regular expressions from readable code. For example, let's say we had this regex:

^(http)(s)?(://)([^\ ]*)$

This regex is designed to match simple URLs. Here's the rules for matching:

The URL must start with either "http" or "https".

The URL must then have "://".

The URL can then have anything following "://", as long as it is isn't a space.

VerbalExpressions allows us to write the following C# code to produce this regex:

Testing the Generated Expressions

Let's say we don't trust this package and want to prove that it is creating regexs that actually match appropriate input. For simple testing, we can use Assert. Let's test all three of the above regexes:

Easy enough, right? I'd like to see more complex testing examples, so if anyone out there comes up with some, let me know!

In addition to testing, one of the cooler little things about VerbalExpressions is that, during debugging, the actual generated regexes are shown for the instances of VerbalExpressions in Visual Studio's debugger windows:

An Important NuGet Note

As of this writing, there is a NuGet package for the C# edition of VerbalExpressions, but the package is woefully behind the most recent version of the code on GitHub. Here's hoping the creator of the package gets this on NuGet so we can use it from there. For this demo, I just downloaded and included the code files in my project (there are only two of them).

Summary

Regular Expressions still suck, but now they suck less (at least in C#) thanks to VerbalExpressions! Use this package to build readable, easy-to-understand regular expressions that can still be used in everyday coding.

I have a sample project for this post over on GitHub, so go check it out!

As always, if I missed something or the code can be made better, feel free to let me know in the comments. If you hate regular expressions, feel free to vent your anger below.

Happy Coding!

NOTE: The example regular expression at the beginning of this post is for parsing IPV4 addresses, taken from this StackOverflow answer.