Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
正则表达式面临问题
2013-05-29 15:15:12我匹配的字符串/^(\d{4})-(\d{2})-(\d{2})({\s}+(\d{2}):(\d{2}):(\d{2}))?$/withpreg_match但不验证...它的给定false。
2013-05-29 15:15:12
/^(\d{4})-(\d{2})-(\d{2})({\s}+(\d{2}):(\d{2}):(\d{2}))?$/
preg_match
false
什么应该是匹配2013-05-29 15:15:12或2013-05-29模式的正则表达式。
让我们先看看你的正则表达式。在您匹配的日期和时间之间{\s}+。这意味着“字符{,后跟空格/制表符,后跟一个或多个}'s ”。
{\s}+
{
}
替换{\s}为?:\s+(匹配一个或多个空格/制表符的非捕获组),以便完整的正则表达式为
{\s}
?:\s+
^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}):(\d{2}):(\d{2}))?$
演示
{\s}+是错的。应该是\s+。花括号仅用作量词或文字。
\s+