Can someone tell me what I need to change in the following regular expression so that the first digit of the month and day is optional, but if present, it must be one of the specified values.
In the code below, the first and second invalid values ("21/16/1908", and "11/42/1908") pass the test, but shouldn't. I know there are a bunch of date related regular expressions out there I could borrow, but I want to learn by understanding what is wrong with my expression.
The expression is for dates in mm/dd/yyyy format where the first digit of the day and month is optional and the first two digits of the year is optional.
Your pattern is more complicated than it needs to be and is perhaps confusing you. try "(0[1-9]|1[0-2])[/-_\\.](0[1-9]|[1-2][0-9]|3[0-1])[/-_\\.]((19|20)?[0-9][0-9])" [ December 26, 2008: Message edited by: Peter Lawrey ]
E.g. If IllegalArgumentException is been thrown, the date is invalid. If you're using Strings, you can also use SimpleDateFormat#setLenient() for that.
For the regex provided by Peter. "-"(hyphen) carries a special meaning in java regex, i.e. a range. So you need to escape the - using \(forward slash) (For any reserved character you need the same) that would mean if you want to use hyphen as a delimiter just as you used for "." (dot) So you regex will become
Thanks to each of you for your response. I also agree that there are better ways to validate a date than using a regex. I am only doing this as a refresher in regex for my own benefit, not to use it in production code. So, to continue the line of questioning, the regex now doesn't work with "1/16/1908". It must not consider the first digit of the month to be optional. I thought that was done by putting * after the grouping.
I figured it out. The month needed to be: ([0-9]|0[1-9]|1[0-2]), and the day needed to be: ([0-9]|0[1-9]|[1-2][0-9]|3[0-1])
All the tests work now. I realize this really doesn't ensure a proper date, I was just using this example to brush up on how to write regular expressions. I don't use them much on the job, but you never know when you might need to. Here is the complete expression:
Originally posted by Sunil Kumar: I agree with Bauke.
For the regex provided by Peter. "-"(hyphen) carries a special meaning in java regex, i.e. a range. So you need to escape the - using \(forward slash) (For any reserved character you need the same) that would mean if you want to use hyphen as a delimiter just as you used for "." (dot) So you regex will become
Note that inside a character class, the normal regex meta character loose their "special powers". So the class:
can be written as:
because the DOT doesn't match "any character", just the '.' itself.
Also note that the hyphen is treated as a "range operator" only when NOT placed at the start or end of a character class. So there's no need to escape the hyphen when doing:
which makes it (a bit) easier to read. [ December 27, 2008: Message edited by: Piet Verdriet ]
Sunil Kumar
Ranch Hand
Joined: Apr 24, 2007
Posts: 76
posted
0
Thanks Piet, that was a useful piece of information about hyphen (range operator)
Piet Verdriet
Ranch Hand
Joined: Feb 25, 2006
Posts: 266
posted
0
Originally posted by Sunil Kumar: Thanks Piet, that was a useful piece of information about hyphen (range operator)
That regex will match "0/0/00". The year part is okay, but the month and day parts should match a leading zero only if it's followed by another, non-zero digit: