Reputation: 16870
I want to match a date-format in PHP using a regular expression. To keep it simple, it can be extended later anyway, the regex should also match 2012-30-60
(which is not a valid date).
My expression is the following: "/\s*\d{4}-\d{2}-\d{2}\s*/i"
. Using preg_match
works fine, but it also matches dates that have preceding and appending characters, like "Hello 2012-09-01 Goodbye."
How can I make the regex to match only when the expression matches the whole string?
Upvotes: 0
Views: 138
Reputation: 851
Try adding ^ and $ at beginning and end of your regexp
"/^\d{4}-\d{2}-\d{2}$/i"
Upvotes: 1
Reputation: 1920
"/^\d{4}-\d{2}-\d{2}$/i"
^ means "begin of string/line"
$ means "end of string/line"
Upvotes: 2