Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

date validation via regex

3 views
Skip to first unread message

Yitzchak Scott-Thoennes

unread,
Nov 15, 2011, 2:01:48 PM11/15/11
to f...@perl.org
This is something people often ask for. The stock answer is that
regexes aren't the correct solution, but really it's not so hard.

This validates Gregorian MM/DD/CCYY dates, for example:

qr#^
(?: 0[1-9] | 1[012] )
/
(?:
0[1-9] | 1[0-9] | 2[0-8]
| (?<! 0[2469]/ | 11/ ) 31
| (?<! 02/ ) 30
| (?<! 02/
(?= ...
(?:
.. (?: [02468][1235679] | [13579][01345789] )
| (?: [02468][1235679] | [13579][01345789] ) 00
)
)
) 29
)
/
[0-9]{4}
\z
#x

Brian Fraser

unread,
Nov 15, 2011, 2:10:32 PM11/15/11
to Yitzchak Scott-Thoennes, f...@perl.org
No. Just, no.
Read this:
http://stackoverflow.com/questions/4077896/perl-or-python-convert-date-from-dd-mm-yyyy-to-yyyy-mm-dd/4078817#4078817
Then forget about using regexen and find a module to do it for you.
Regexp::Common::time is pretty nice, albeit slow if you are doing millions
of validations.

(But in the spirit of this list, sure, that's cool. Would be cooler if you
could abstract it using (?(DEFINE)) :)

Yitzchak Scott-Thoennes

unread,
Nov 15, 2011, 4:47:07 PM11/15/11
to Brian Fraser, f...@perl.org
On Tue, Nov 15, 2011 at 11:10 AM, Brian Fraser <fras...@gmail.com> wrote:
> No. Just, no.

On this list, I think that counts as high praise. Thanks!

> Read this:
> http://stackoverflow.com/questions/4077896/perl-or-python-convert-date-from-dd-mm-yyyy-to-yyyy-mm-dd/4078817#4078817

Err, which part? tchrist is an expert on dealing with unicode in
various languages/regex engines and in real-world "text" data, but I
think he's overreaching there; I don't expect my date strings to
include non-ASCII digits. I can see some software butchering - into a
non-ASCII dash character, but I'd tend to treat fixing that as a
separate problem. (Though that was actually a motivation in having my
regex looking for /, not -.)

> Then forget about using regexen and find a module to do it for you.
> Regexp::Common::time is pretty nice, albeit slow if you are doing millions
> of validations.
> (But in the spirit of this list, sure, that's cool. Would be cooler if you
> could abstract it using (?(DEFINE)) :)

Meh. I think that would only obfuscate it.

Stefan `Sec` Zehl

unread,
Nov 16, 2011, 5:15:07 AM11/16/11
to Yitzchak Scott-Thoennes, f...@perl.org
Hi,

On Tue, Nov 15, 2011 at 11:01 -0800, Yitzchak Scott-Thoennes wrote:
> This is something people often ask for. The stock answer is that
> regexes aren't the correct solution, but really it's not so hard.
>
> This validates Gregorian MM/DD/CCYY dates, for example:

Uh. mittle endian dates are really a pain. Can't we all standardize on
something sensible like ccyy-mm-dd? It even sorts nice :-)

CU,
Sec
--
<inof> I don't have anything against penguins, as long as they stay in the
Antarctica, swim around, catch fishes and do the usual penguin stuff.
0 new messages