Regular Expression to match valid dates

后端 未结 15 1842
庸人自扰
庸人自扰 2020-11-22 04:48

I\'m trying to write a regular expression that validates a date. The regex needs to match the following

  • M/D/YYYY
  • MM/DD/YYYY
  • Single digit mon
相关标签:
15条回答
  • 2020-11-22 05:21

    if you didn't get those above suggestions working, I use this, as it gets any date I ran this expression through 50 links, and it got all the dates on each page.

    ^20\d\d-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(0[1-9]|[1-2][0-9]|3[01])$ 
    
    0 讨论(0)
  • 2020-11-22 05:22

    This regex validates dates between 01-01-2000 and 12-31-2099 with matching separators.

    ^(0[1-9]|1[012])([- /.])(0[1-9]|[12][0-9]|3[01])\2(19|20)\d\d$
    
    0 讨论(0)
  • 2020-11-22 05:22

    If you're going to insist on doing this with a regular expression, I'd recommend something like:

    ( (0?1|0?3| <...> |10|11|12) / (0?1| <...> |30|31) |
      0?2 / (0?1| <...> |28|29) ) 
    / (19|20)[0-9]{2}
    

    This might make it possible to read and understand.

    0 讨论(0)
提交回复
热议问题