1

I am playing around with regular expression for trying to recognize dates and I have the following regular expression:

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

The issue is that when I have an incorrect date (in America at least) like 14/02/2000 is still thinks 4/02/2000 is a date. How can I modify my regular expression so that it doesn't recognize part of the number?

user2604504
  • 669
  • 2
  • 14
  • 29

2 Answers2

1

You can wrap the regular expression in word boundaries: \b, to indicate that there should be a non-word character before the first number.

Vasili Syrakis
  • 8,340
  • 1
  • 31
  • 51
0

How about: ^(([0]?[1-9])|([1][0-2]))\/([012]?[0-9]|[3][01])\/[12][0-9][0-9][0-9]$ I added the ^ and $, and got rid of the ^([1][3-9]) at the first segment, I don't understand what is its purpose. I also escaped the slashes (\/)

Uri Y
  • 830
  • 4
  • 11