I haven't posted a "WTF" Regular Expression before today because RegEx's are a fairly complex animal. Well, that and I haven't gotten a submission for one. They're incredibly useful tools for parsing and validation, but I think most (including myself) are glad that there are places like RegExLib.com around to save us from the intricacies of lazy quantifiers, backtracking, and lookbehinds.

There are definitely a few out there who will brave the trenches and roll their own expression. After looking at Matt Brown's colleague's 347-character-long, partially-working "mm/dd/yyyy" date validator (used, no less in a language with built in support for date validation), it's pretty clear that not everyone belongs on the front line ...

# ED: Line breaks added
^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)
(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)
(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$
|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:20)?(?:0[48]|[2468][048]|[13579][26]))$

For you regex junkies, Matt explains more ...

I'm not sure if my favorite part is the use of non-capturing groupings ("(?: )") without any actual backreferences, with the mixing of capturing groups ("( )"), or if my favorite part is that the year section of the regular expression is:

([1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])

...which ends up allowing any four digits EXCEPT 0000. But 0001 is valid.


And if you haven't voted for a the logo already, please take a moment to vote. No login or anything required. There's a good amount of votes in already, so I will probably close the "polls" soon.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!