Regular Expression:Why doesn't this work?

Discussion in 'PHP' started by locus, Feb 26, 2007.

  1. #1
    /^(20)(?(1)(0[7-9]|[^0]\d)|(21)\d\d)$/
    PHP:
    I am trying to get this conditional expression to match dates between 2007 and 2199.

    If i try 2007, or anything of the form 20_ _, it works, when i try any year of the form 21_ _, it does not work

    Could you help me with this? it would be greatly appreciated.

    Thanks for your attention
     
    locus, Feb 26, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    /^2(0|1)(0[7-9]|[1-9][0-9])$/

    Although I don't really understand why you'd want to do it like this? Judging by the ^ and $, the string only contains the number anyway, in which case a simple greater than 2007 and less than 2199 would be much quicker.
     
    rodney88, Feb 26, 2007 IP
  3. locus

    locus Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am validating a whole date from a form text box, just could not get the year part working.

    I wanted to learn something from that expression i posted, since it did not work.

    Thanks for your suggestion,, i really appreciate it.
     
    locus, Feb 26, 2007 IP
  4. locus

    locus Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Unfortunately the pattern you gave does not work, it does not validate 2101, and some others.

    I found one that does though:

    '/^2(00[7-9])|(0[1-9]\d)|(1\d\d)$/'
    PHP:
    Thanks again.
     
    locus, Feb 26, 2007 IP