Re: matching date



joez...@xxxxxxxxx wrote:
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds.

You have multiple problems.
1) if you use / as the delimiter, you have to escape (that is, precede
with a backslash) any actual / characters that you want to match.
2) commas match themselves in character classes. A character class is
either a list (with nothing separating the elements) or a range (using
the - character) or a combination of both
3) Not all of your character classes are terminated
4) The character class [0-9] is more easily written \d

Putting those together:
m/[01]\d\/[0-3]\d\/\d\d/;

However, this is still not sufficient to match a real date, as it would
allow a date of, for example,
19/38/04;

Could some one show me the correct way
to pattern match on this?

The correct way is to not reinvent the wheel:

use Regexp::Common qw/time/;
/$RE{time}{mdy}/;
to allow any month-day-year pattern, or
/$RE{time}{m2d2y2}/
to specify exactly two digits for each field.

Paul Lalli

.



Relevant Pages

  • Re: Use of uninitialized value in pattern match (m//)
    ... Use of uninitialized value in pattern match at a.pl line 15. ... unshift @a,$n; ... Because there is no such character in the target string ...
    (perl.beginners)
  • Re: Extrating String using Split
    ... > I get directory tree A, ... Your problem is that a pattern match imposes double-quotish ... seen as a \T character, instead of a backslash followed by a T. If you ... the pattern you're passing to the pattern match. ...
    (comp.lang.perl.misc)
  • Spurious "Use of uninitialized value" with -w, redux
    ... OK, my first post had a bug, but it's a real problem, honest. ... Use of uninitialized value in pattern match at unix_replica.pl ... Line 209 guarantees that there's at least one character in $fsmount, ...
    (comp.lang.perl.misc)
  • Re: Extract the integer value from a string (beginner)
    ... All you are malloc'ing there is enough for *one* character. ... /* Only the ascii char 0-9 is assigned to tmp (correct me if I am ... that tries to access the negative-9th element of the array 'data'. ... It looks like you are trying to pattern match there. ...
    (comp.lang.c)
  • Re: goblinhack 1.11 released
    ... I'm wanting to add character classes next - and I think some kind of ... ASCII-graphics-based, OpenGL accelerated, rogue-like dungeon game. ...
    (rec.games.roguelike.nethack)