Worst regular expression ever

by Mads Kristensen 16. May 2007 05:28

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

Tags:

Server-side

Comments

5/16/2007 8:46:01 AM #

David Seruyange

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}

David Seruyange United States |

5/16/2007 1:47:44 PM #

Tommy Carlier

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

Tommy Carlier Belgium |

5/16/2007 6:37:21 PM #

Mads Kristensen

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

Mads Kristensen Denmark |

5/16/2007 7:56:22 PM #

KG2V

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 United States |

5/16/2007 8:10:41 PM #

KG2V

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]  

KG2V United States |

5/16/2007 9:15:29 PM #

Mads Kristensen

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

Mads Kristensen Denmark |

5/16/2007 10:06:27 PM #

matthew

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

matthew United States |

5/16/2007 10:30:53 PM #

KG2V

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 United States |

5/17/2007 12:09:21 AM #

KG2V

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])

KG2V United States |

5/18/2007 10:34:08 AM #

Steve Smith

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

Steve Smith United States |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008