[Israel.pm] RegEX Failure

Jason Elbaum jason.elbaum at gmail.com
Sun Sep 24 17:42:17 PDT 2006


Personally, I find it simpler and clearer to separate the handling of
syntax and semantics. First match the structure of the string:

(\d\d)/(\d\d)/(\d{4}) (\d\d):(\d\d):(\d\d)

and then validate the ranges of the numbers separately

$1 <= 11 && $2 <= 31 && .... (etc.)

This has many added advantages:

1. You can produce a clear error message ("Invalid month 13 in date...")
2. You can improve the validation checking ("Invalid day 31 in month
11...", "Year 1066 out of supported range...")
3. You can parse different date syntaxes without changing the
validation code (e.g. put the captures in named variables - $day,
$month, $year, etc.)
4. Your code becomes clear and obviously correct!

Or, of course, you can use a module such as Date::Parse:

http://search.cpan.org/dist/TimeDate/lib/Date/Parse.pm

Good luck!

Jason Elbaum
Freescale Semiconductor Israel


On 9/22/06, Georges EL OJAIMI <g-ojaimi at cyberia.net.lb> wrote:
> Hello,
>
> This is a sample of valid time:
>
> 09/22/2006 17:59:18
>
> If the user enter 60 for minutes or seconds it is wrong! and so on...
>
> Best regards,
> Georges
>
> >
> > From: "Gabor Szabo" <szabgab at gmail.com>
> > Date: 2006/09/22 Fri PM 05:36:31 EAT
> > To: "Perl in Israel" <perl at perl.org.il>
> > Subject: Re: [Israel.pm] RegEX Failure
> >
> > On 9/22/06, Georges EL OJAIMI <g-ojaimi at cyberia.net.lb> wrote:
> > > Hello,
> > >
> > > I have a Regular expression that check for valid date/time insertion in a database. Sometimes this code verify abnormal time entry and other time fails.
> > > Can you please check what is wrong in it:
> > >
> > > (1[0-2]|0?[1-9])[\\-\\/](0?[1-9]|[1][0-9]|3[01])[\\-\\/]((19|20)\\d{2}) (0?[0-9]|[1][0-9]|[2][0-3]):(0?[0-9]|[1-5][0-9]):(0?[0-9]|[1-5][0-9])
> >
> > It is very hard to check such a regex, especially without examples of
> > input strins. Maybe you could share with us examples of strings that that
> > are recognized correctly and examples that are not recognized correctly.
> >
> > In any case you could start to split this up. Using /x option you can
> > add spaces and
> > comments.
> >
> > If I am not mistaken it would be like this:
> >
> > /
> > (1[0-2]|0?[1-9])                          # add comments here
> > [\\-\\/]
> > (0?[1-9]|[1][0-9]|3[01])
> > [\\-\\/]                                       # should this be [\\\/-] instead?
> > ((19|20)\\d{2})                            # why double escape \\d  ?
> > [ ]                                            # is this a real space?
> > (0?[0-9]|[1][0-9]|[2][0-3])
> > :
> > (0?[0-9]|[1-5][0-9])
> > :
> > (0?[0-9]|[1-5][0-9])
> > /x
> >
> > Gabor
> > _______________________________________________
> > Perl mailing list
> > Perl at perl.org.il
> > http://perl.org.il/mailman/listinfo/perl
> >
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



More information about the Perl mailing list