PHP regular expression issue preg_match() on timestamps
Facing issue with regular expression
2013-05-29 15:15:12
string I am matching with /^(d{4})-(d{2})-(d{2})({s}+(d{2}):(d{2}):(d{2}))?$/
with preg_match
but not validating … its giving false
.
What should be regexp to match 2013-05-29 15:15:12 or 2013-05-29 pattern.
Let’s take a look at your regex first. Between the date and the time you’re matching {s}+
. This means “the character {
, followed by a space/tab, followed by one or more }
‘s“.
Replace {s}
with ?:s+
(a non capturing group matching one or more spaces/tabs) so the full regex is
^(d{4})-(d{2})-(d{2})(?:s+(d{2}):(d{2}):(d{2}))?$
DEMO
The {s}+
is wrong. It should be s+
. The curly brackets are used as quantifiers or literals only.