简体   繁体   English

正则表达式从任何字符串获取日期yyyy-mm-dd

[英]regex to get date yyyy-mm-dd from any string

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. 首先请原谅我不是那么熟悉正则表达式。我想要的是一个正则表达式,它将从任何类型的字符串中提取类似mysql日期的日期。 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])$ 到现在为止我使用它: ^[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]). 但是现在我想从其他字符串和日期时间字符串中提取日期模式我尝试将正则表达式更改为^[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. 有时它给了我一个3位数的结果。

In other words sting starts with, yyyy-mm-dd and is followed up by spaces characters numbers or anything. 换句话说,刺痛开始于, yyyy-mm-dd ,后跟空格字符数字或任何东西。 How do I extract the date? 如何提取日期?

UPDATE 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])

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. 如果你的字符串有多个日期格式值出现,并且你想要捕获所有这些值,你应该使用preg_match_all函数,如果它不是preg_match就足够了。 Also using ^ and $ means input string is should be just a date, so avoid it. 另外使用^$表示输入字符串应该只是一个日期,所以要避免它。

<?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
)
*/

Live demo 现场演示

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. 这是返回三位数日的比赛的原因。

Try this: you can use preg_match() or preg_match_all() 试试这个:你可以使用preg_match()preg_match_all()

   $dateArray = preg_match("/(\d{4}-\d{2}-\d{2})/", $str, $match);

Then use strtotime and date 然后使用strtotime日期

$date = date('Y-m-d',strtotime($match[0]));

只需为\\b替换^

\b(\d{4}-\d{2}-\d{2})

它是你的正则表达式末尾的点(它匹配任何字符,除了换行符)尝试删除它

^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM