Forum Moderators: coopster

Message Too Old, No Replies

regular expression date format matching

         

snehula

1:33 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



Hi there,

those regular expressions.. trying to get the hang of it.
I want the user to enter date in the format dd/mm/yyyy.
here is the regular expression that I use to match the date with:

$regexp = '/^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20[0-9]{2})$/';


I sure can't see what the mistake is but preg_match($regexp, $somedate) keeps returning false (although I got 0 once or twice) for any date that's in the format above (dd/mm/yyyy). So, where is the mistake?

Another thing I'd love to know is, as far as i know the forward slash in the end and beginning is a delimiter. Could i use any other character instead the slash?

brotherhood of LAN

1:39 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Could i use any other character instead the slash?


Yes, I usually use a single quote but the convention does seem to be forward slash (a convention from other languages i think)

$regexp = '/^([0-9]{2}\/){2}(?:19|20)[0-9]{2}$/';

Try that one.

  • Match 2 numbers and forward slash twice (dd/mm/)
  • Match 19|20 (yy)
  • Match 2 Numbers [yy]

    That does insist that a 0 is placed for days and months like 01, 02 etc which may not be as intuitive as you'd like for user input.
  • g1smd

    6:23 pm on Jul 7, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    [\/]
    is garbage.

    Don't escape the slashes.

    Do choose a different end-point character. It MUST be different to any of the characters in the RegEx pattern.

    $regexp = '#^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20[0-9]{2})$#';

    Readie

    8:08 pm on Jul 7, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Little more friendly... Doesn't insist on a leading 0, and makes sure days can't go above 31:

    !^(0?\d|[12]\d|3[01])/(0?\d|1[012])/((?:19|20)\d{2})$!


    Also, you made a tiny mistake g1msd:

    (19|20[0-9]{2})


    Effectively:

    19 or 20xx

    g1smd

    8:17 pm on Jul 7, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Apologies. I got the parentheses in the wrong place, copying and pasting from the OP. My eye was on the other errors.

    Use
    (19|20)[0-9]{2}

    lucy24

    8:26 pm on Jul 7, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    (0[1-9]|[12][0-9]|3[01])/

    In something this short, saying

    (0?[1-9]|[12][0-9]|3[01])/

    shouldn't place an undue burden on your computer. Or you can do an on-the-fly rewrite of
    ([1-9])/ to 0$1/
    That's assuming you take the radical position of making the computer subordinate to the human ;)

    Is there a problem with \d instead of [0-9] ?

    Oh, and will it be routed via something that filters out spurious dates like 31/04 ? Or 29/02 in years that didn't have one? (Fortunately 2000 did, so a simple "multiple of 4" function will pick up all of them.) Since you're talking about user input, a dropdown selector for the month might be safer, so you can be sure what "07/11" means. 12 items, as for months, is about the limit on dropdowns; go to 30 or more and it gets annoying.

    g1smd

    8:31 pm on Jul 7, 2011 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Use
    \d
    if your system uses PCRE RegEx, or
    [0-9]
    if you want POSIX.