Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    Stupid preg_match pattern

    I'm trying to check if a date matches a pattern (DD/MM/YY) using preg_match and I can't figure out the damn proper pattern. I've read all over about modifiers and the delimiters and I still can't get it to work.

    My pattern so far...
    PHP Code:
    $datePattern "^[0-9]{2}\/[0-9]{2}\/([0-9]{2}$"//Format DD/MM/YY 
    Seems like it should be extremely simple but I could never wrap my head around regular expressions, thus why I try to avoid them as much as possible. I thought {2} was supposed to determine how long the string is allowed to be but according to PCRE manual it means
    {n} Match exactly n times.
    Frustrating :/

    Can someone help me out?

    Edit-
    I tried some grouping w/() but still couldn't get it to work. Grr... I have no idea
    Last edited by fl00d; 08-08-2008 at 05:57 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by fl00d View Post
    I'm trying to check if a date matches a pattern (DD/MM/YY) using preg_match and I can't figure out the damn proper pattern. I've read all over about modifiers and the delimiters and I still can't get it to work.

    My pattern so far...
    PHP Code:
    $datePattern "^[0-9]{2}\/[0-9]{2}\/([0-9]{2}$"//Format DD/MM/YY 
    Seems like it should be extremely simple but I could never wrap my head around regular expressions, thus why I try to avoid them as much as possible. I thought {2} was supposed to determine how long the string is allowed to be but according to PCRE manual it means
    {n} Match exactly n times.
    Frustrating :/

    Can someone help me out?

    Edit-
    I tried some grouping w/() but still couldn't get it to work. Grr... I have no idea
    PHP Code:
    $dateval "08/12/2008"// work with this format
    $dateval "08/12/08"// or this
    preg_match("/^(\d{2})\/(\d{2})\/(\d{2,4})$/",$dateval,$res);
    print_r($res); 
    regards

  • Users who have thanked oesxyl for this post:

    fl00d (08-08-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    Thank you very much oesxyl! I don't understand why I don't understand regular expression implementation... it's a bit weird.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by fl00d View Post
    Thank you very much oesxyl! I don't understand why I don't understand regular expression implementation... it's a bit weird.
    , is same thing like math, nothing to understand just apply the given rules.
    1. use the classes from the kind of engine you use, keep in mind differences and common thing
    2. take from left to right, don't think but the current position
    3. build step by step and test

    I lie if I say that I understand something,

    regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •