Hello SO:
I have this Regular Expression that matches the following strings:
<!-- 09-02-2009 --->
<!-- 09-02-2009 12:00:00 --->
<!-- 09-02-2009 12:00:00 A --->
<!-- 09-02-2009 12:00:00 AM --->
Here is the pattern:
<!-- (?<month>\d{2}?)-(?<day>\d{2}?)-(?<year>\d{4}?)(?:(?: ?\d{2}:?){3}?(?: ?[aApP][mM]?)?)? --->
updated pattern, per twistol:
<!-- (?<month>\d{2}?)-(?<day>\d{2}?)-(?<year>\d{4}?)(?<time>(?: ?(?:\d{2}:){2}\d{2})?(?: ?[aApP][mM]?)?)? --->
Is there anything I can do to simplify this pattern?
Thanks!
EDIT
Here is the pattern I came up with all comments/answers, plus validation built in. It is a bit ugly, but who said regex needs to be pretty? :P
<!-- (?<month>(?:0[1-9]|1[0-2]))-(?<day>(?:0[1-9]|1[0-9]|2[0-9]|3[01]))-(?<year>\d{4})(?<time> (?:0[0-9]|1[0-9]|2[0-3]):(?:[0-5][0-9])(?::[0-5][0-9])?(?: [aApP][mM]?)?)? --->
It will match valid dates in the following formats:
<!-- 09-02-2009 --->
<!-- 09-02-2009 12:00 --->
<!-- 09-02-2009 12:00 A --->
<!-- 09-02-2009 12:00 AM --->
<!-- 09-02-2009 12:00:00 --->
<!-- 09-02-2009 12:00:00 A --->
<!-- 09-02-2009 12:00:00 AM --->