php 正则表达式从任何字符串中获取日期 yyyy-mm-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19564063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
regex to get date yyyy-mm-dd from any string
提问by syd619
first of all excuse me for not being regex familiar that much.What I Would like is a regex that will extract a date like mysql date from any type of string.
Until now I was using this : ^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$
首先请原谅我对正则表达式不太熟悉。我想要的是一个正则表达式,它可以从任何类型的字符串中提取像 mysql 日期这样的日期。直到现在我一直在使用这个:^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$
However now I want to extract date patterns from other strings and datetime strings I tried altering the regex to ^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]).
based on some online regex testers, but it fails. Also some times it gave me a result with a 3 digit day.
但是,现在我想从其他字符串和日期时间字符串中提取日期模式,我尝试将正则表达式更改为^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]).
基于一些在线正则表达式测试器,但它失败了。有时它也给了我一个 3 位数天的结果。
In other words sting starts with, yyyy-mm-dd
and is followed up by spaces characters numbers or anything. How do I extract the date?
换句话说,stingyyyy-mm-dd
以空格开头,然后是空格字符数字或任何东西。我如何提取日期?
UPDATE
更新
I'm testing regex with preg_match here: http://www.pagecolumn.com/tool/pregtest.htm
我在这里用 preg_match 测试正则表达式:http: //www.pagecolumn.com/tool/pregtest.htm
so far the only thing that seems to work is
到目前为止,似乎唯一有效的是
[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])
采纳答案by Jon
To match dates wherever they appear, remove the $
and ^
anchors from your original regex.
要匹配出现在任何地方的日期,请从原始正则表达式中删除$
和^
锚点。
To match dates at the start of any input remove the $
at the end (leave the ^
).
要在任何输入的开头匹配日期,请删除$
末尾的(保留^
)。
You can also put the remaining pattern inside parentheses for convenience, so that the match is also captured as a whole.
为方便起见,您还可以将剩余的模式放在括号内,以便将匹配也作为一个整体进行捕获。
Your suggested improvement has a spurious dot at the end which will match any character; that was the reason for returning matches with three-digit days.
您建议的改进在末尾有一个伪点,可以匹配任何字符;这就是返回三位数天数的比赛的原因。
回答by revo
If your string has more than one date form value occurrence and you wanna capture all of them you should use preg_match_all
function and if it's not preg_match
is enough. Also using ^
and $
means input string is should be just a date, so avoid it.
如果您的字符串有多个日期形式值出现并且您想捕获所有这些,您应该使用preg_match_all
函数,如果还preg_match
不够。同样使用^
and$
意味着输入字符串应该只是一个日期,所以避免它。
<?php
$input_string = "yes today is 2013-10-24";
if(preg_match("/\d{4}-\d{2}-\d{2}/", $input_string, $match))
{
print_r($match);
}
else
echo "not matched";
////////////////////////
/* Output:
Array
(
[0] => 2013-10-24
)
*/
回答by Sunil Kumar
回答by edi_allen
Just replace ^
for \b
.
只需替换^
为\b
.
\b(\d{4}-\d{2}-\d{2})
回答by Mizax
It is dot in the end of your regexp (it matches any character, except for line breaks) Try removing it
它是正则表达式末尾的点(它匹配任何字符,换行符除外)尝试删除它
^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])