Im currently working in a grails project and I ended up to a problem regarding matches constraints in grails. My field should only accept a String with a date-like format exactly like this:
10-25-2012 5:00PM
Is this possible in matches constraint using regex? I'm always having a hard time in data filtering using regex cause it's a little bit confusing.
Is there no Date object you can use? I don't know, but I can help you with the regex:
Constructing a regex is not difficult and especially in your case straight forward:
^
Matches the start of the string$
Matches the end of the string\d
is a digit{2}
is a quantifier that makes the previous character required 2 times[AP]
is a character class that matchesA
orP
This regex just checks the format, not if the digits represent a valid Date or Time! (e.g. 99-99-0000 35:61PM is valid)
Read my Blog post What absolutely every Programmer should know about regular expressions for some more brief information.