php 日期时间的正则表达式模式是什么 (2008-09-01 12:35:45 )?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37732/
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
What is the regex pattern for datetime (2008-09-01 12:35:45 )?
提问by Alexander Morland
What is the RegEx pattern for DateTime (2008-09-01 12:35:45 ) ?
DateTime (2008-09-01 12:35:45) 的 RegEx 模式是什么?
I get this error:
我收到此错误:
No ending delimiter '^' found
未找到结束分隔符“^”
Using:
使用:
preg_match('(?n:^(?=\d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2)(?=.{3,4}(1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|0?[1-9]|1\d|2[0-8])(?<sep>[/.-])(?<month>0?[1-9]|1[012])(?<year>(1[6-9]|[2-9]\d)\d{2})(?:(?=\x20\d)\x20|$))?(?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(?i:\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)', '2008-09-01 12:35:45');
Gives this error:
给出这个错误:
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 0 in E:\www\index.php on line 19
警告:preg_match() [function.preg-match]:编译失败:第 19 行 E:\www\index.php 中的偏移量 0 处没有重复
回答by Greg Hewgill
@Espo: I just have to say that regex is incredible. I'd hate to have to write the code that did something useful with the matches, such as if you wanted to actually find out what date and time the user typed.
@Espo:我只能说正则表达式令人难以置信。我不想编写对匹配有用的代码,例如,如果您想实际找出用户键入的日期和时间。
It seems like Tom's solution would be more tenable, as it is about a zillion times simpler and with the addition of some parentheses you can easily get at the values the user typed:
似乎 Tom 的解决方案更有效,因为它简单了无数倍,并且添加了一些括号,您可以轻松获得用户键入的值:
(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})
If you're using perl, then you can get the values out with something like this:
如果您使用的是 perl,那么您可以使用以下内容获取值:
$year = ;
$month = ;
$day = ;
$hour = ;
$minute = ;
$second = ;
Other languages will have a similar capability. Note that you will need to make some minor mods to the regex if you want to accept values such as single-digit months.
其他语言也有类似的能力。请注意,如果您想接受诸如单位数月份之类的值,则需要对正则表达式进行一些小修改。
回答by Tom
A simple version that will work for the format mentioned, but not all the others as per @Espos:
一个简单的版本,适用于提到的格式,但不是@Espos 的所有其他格式:
(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})
回答by Espo
http://regexlib.com/REDetails.aspx?regexp_id=610
http://regexlib.com/REDetails.aspx?regexp_id=610
^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$
^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(? =.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][ 26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]| 1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2 -9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0- 5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}) ?$
This RE validates both dates and/or times patterns. Days in Feb. are also validated for Leap years. Dates: in dd/mm/yyyy or d/m/yy format between 1/1/1600 - 31/12/9999. Leading zeroes are optional. Date separators can be either matching dashes(-), slashes(/) or periods(.) Times: in the hh:MM:ss AM/PM 12 hour format (12:00 AM - 11:59:59 PM) or hh:MM:ss military time format (00:00:00 - 23:59:59). The 12 hour time format: 1) may have a leading zero for the hour. 2) Minutes and seconds are optional for the 12 hour format 3) AM or PM is required and case sensitive. Military time 1) must have a leading zero for all hours less than 10. 2) Minutes are manditory. 3) seconds are optional. Datetimes: combination of the above formats. A date first then a time separated by a space. ex) dd/mm/yyyy hh:MM:ss
此 RE 验证日期和/或时间模式。2 月的天数也适用于闰年。日期:在 1/1/1600 - 31/12/9999 之间采用 dd/mm/yyyy 或 d/m/yy 格式。前导零是可选的。日期分隔符可以匹配破折号 (-)、斜杠 (/) 或句点 (.) 时间:采用 hh:MM:ss AM/PM 12 小时格式(12:00 AM - 11:59:59 PM)或 hh :MM:ss 军用时间格式(00:00:00 - 23:59:59)。12 小时制时间格式:1) 小时可能有一个前导零。2) 分钟和秒对于 12 小时格式是可选的 3) AM 或 PM 是必需的并且区分大小写。军用时间 1) 对于所有小于 10 的小时,必须有一个前导零。2) 分钟是强制性的。3) 秒是可选的。日期时间:上述格式的组合。先是日期,然后是由空格分隔的时间。例如) dd/mm/yyyy hh:MM:ss
Edit: Make sure you copy the RegEx from the regexlib.com website as StackOverflow sometimes removes/destroys special chars.
编辑:确保您从 regexlib.com 网站复制 RegEx,因为 StackOverflow 有时会删除/销毁特殊字符。
回答by Philip
$date = "2014-04-01 12:00:00";
preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/',$date, $matches);
print_r($matches);
$matches will be:
$matches 将是:
Array (
[0] => 2014-04-01 12:00:00
[1] => 2014
[2] => 04
[3] => 01
[4] => 12
[5] => 00
[6] => 00
)
An easy way to break up a datetime formated string.
一种分解日期时间格式字符串的简单方法。
回答by Javad Yousefi
^([2][0]\d{2}\/([0]\d|[1][0-2])\/([0-2]\d|[3][0-1]))$|^([2][0]\d{2}\/([0]\d|[1][0-2])\/([0-2]\d|[3][0-1])\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)$
回答by deni
regarding to Imran answer from Sep 1th 2008 at 12:33 there is a missing : in the pattern the correct patterns are
关于伊姆兰 2008 年 9 月 1 日 12:33 的回答,有一个缺失:在模式中,正确的模式是
preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', '2008-09-01 12:35:45', $m1);
print_r( $m1 );
preg_match('/\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}/', '2008-09-01 12:35:45', $m2);
print_r( $m2 );
preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', '2008-09-01 12:35:45', $m3);
print_r( $m3 );
this returns
这返回
Array ( [0] => 2008-09-01 12:35:45 )
Array ( [0] => 2008-09-01 12:35:45 )
Array ( [0] => 2008-09-01 12:35:45 )
回答by Tornike
Here is my solution:
这是我的解决方案:
/^(2[0-9]{3})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) (0[0-9]|1[0-9]|2[0123])\:([012345][0-9])\:([012345][0-9])$/u
回答by themepark
I have modified the regex pattern from http://regexlib.com/REDetails.aspx?regexp_id=610. The following pattern should match your case.
我已经修改了http://regexlib.com/REDetails.aspx?regexp_id=610 中的正则表达式模式。以下模式应符合您的情况。
^(?=\d)(?:(?:1[6-9]|[2-9]\d)?\d\d([-.\/])(?:1[012]|0?[1-9])(?:31(?<!.(?:0[2469]|11))|(?:30|29)(?<!.02)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS
回答by Milkncookiez
Adding to @Greg Hewgill answer: if you want to be able to match both date-time and onlydate, you can make the "time" part of the regex optional:
添加到@Greg Hewgill 答案:如果您希望能够匹配日期时间和仅日期,您可以将正则表达式的“时间”部分设为可选:
(\d{4})-(\d{2})-(\d{2})( (\d{2}):(\d{2}):(\d{2}))?
this way you will match both 2008-09-01 12:35:42and 2008-09-01
这样你就可以同时匹配2008-09-01 12:35:42和2008-09-01
回答by Yeongjun Kim
Here is my solution:
这是我的解决方案:
[12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) ([01][0-9]|2[0-3]):[0-5]\d



