Worst regular expression ever

May 15, 2007

A couple of days ago, I had to do some simple date and time validation using regex. The format to validate was yyyy-mm-dd hh:mm. It looks simple enough and like always, I thought I could find the regex on the web, but I couldn’t.

I was under a tight deadline and the date/time validation was not that important, so I made a decision to make the worst regex the world have ever seen. Maybe it’s long, ugly and wrong, but it works just enough to make me use it until I have the time to make a better one.

Warning, do not try this at home!

[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]

The good thing about regex is that they are always ugly, most of them are long and only five people in the world understand them enough to see if they work or not.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (10) -

David Seruyange
David Seruyange United States
5/15/2007 11:46:01 PM #

Hey Mads,
I know exactly how you feel. In those time crunch moments I find myself using the mantra "keep your wits about you" as told by <a href="www.cauldwell.net/patrick/blog/">Patrick Caldwell</a>.  Perhaps you can try the following, which you may find cleaner:

\d{4}-\d{2}-\d{2} \d{2}:\d{2}

Tommy Carlier
Tommy Carlier Belgium
5/16/2007 4:47:44 AM #

Couldn't you just have used DateTime.TryParse? Your regular expression also doesn't check for invalid dates (like 2007-13-45 25:78).

Mads Kristensen
Mads Kristensen Denmark
5/16/2007 9:37:21 AM #

Tommy, no I could not in this case. Very unfortunate.

KG2V
KG2V United States
5/16/2007 10:56:22 AM #

At least change it to:

[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]

KG2V
KG2V United States
5/16/2007 11:10:41 AM #

doing some thinking....

For the year part:
[0-9][0-9][0-9][0-9]-  works fine
for the Month
(0?[0-9]|1?[0-2])  should work better - as you wont match stuff like 13
for the Day - now THAT would be fun because of variaing month length, but:
([0-2]?[0-9]|3?[0-1]) should be a bit better (can't enter > 31)

and for 24 hour time

The HOUR part is
([01]?[0-9]|2[0-3])
The Minute would be:
[0-5][0-9]

So - string it together:
[0-9][0-9][0-9][0-9]-(0?[0-9]|1?[0-2])-([0-2]?[0-9]|3?[0-1]) ([01]?[0-9]|2[0-3]):[0-5][0-9]  

Mads Kristensen
Mads Kristensen Denmark
5/16/2007 12:15:29 PM #

KG2V, That's way better but it does not check for february 30th which doesn't exist and what about leap years? It's a damned regex that can do a complete date/time validation, but yours is a step in the right direction. Thanks

matthew
matthew United States
5/16/2007 1:06:27 PM #

Did you check out regexlib.com?  It looks like they may have had at least one that would match what you needed.

http://regexlib.com/REDetails.aspx?regexp_id=760

KG2V
KG2V United States
5/16/2007 1:30:53 PM #

Yeah - I did say day of month would be "fun" - and I didn't trap for that at all - in fact, besides the Feb problem, you have the April, June, Sept, Nov problem

KG2V
KG2V United States
5/16/2007 3:09:21 PM #

I just noticed that for the MONTH part I had

(0?[0-9]|1?[0-2])

but that can lead to a month 0 - ooops

(0?[1-9]|1?[0-2])

Steve Smith
Steve Smith United States
5/18/2007 1:34:08 AM #

And if http://regexlib.com/ didn't have what you needed, don't forget http://regexadvice.com where you can get regex help.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.