Reputation:
The formats I'm looking to catch using preg_match_all()
:
The regex I have so far:
/((\d{1,2}|\d{4})(.\d{1,2}))(am|pm)*/i
Any help much appreciated, I can't seem to catch 5 & 6 with current regex.
Upvotes: 0
Views: 72
Reputation: 91508
How about:
/^(\d{1,2})([.:]?\d{1,2})?(am|pm)?$/i
Explanation:
/ : regex delimiter
^ : begining of string
( : capture group 1
\d{1,2} : one or two digit
) : end group 1
( : capture group 2
[.:]? : optional . or :
\d{1,2} : one or two digit
), : end group 2 otional
(am|pm)? : am or pm optional
$ : end of string
/i : regex delimiter with caseinsensitive flag
Upvotes: 1
Reputation: 785761
You can use this regex:
/\b(\d{1,2})([.:]?\d{1,2})?([ap]m)?\b/i
Breakup of the regex:
\b # word boundary
\d{1,2} # match 1 or 2 digits for hours
[.:]? # match *optional* dot or colon i.e. minute separator
\d{1,2} # match 1 or 2 digits for minutes
([ap]m)? # match *optional* am/pm
\b # word boundary
Upvotes: 1