如何在字符串中找到这种格式的日期?

所以我有这些字符串:

 1. This is some 734 random 6272834 data 04 FEB 1:01 and maybe some here
                                         ^^^^^^^^^^^
 2. This is some random 1512 data 03 Jun 18:50
                                  ^^^^^^^^^^^^
 3. 23 APR 05:00 and maybe some here
    ^^^^^^^^^^^^
 4. 13 JUN 13:22
    ^^^^^^^^^^^^

我需要知道那个日期和时间。

我尝试使用explode,然后检查某项是否为数字,然后下一个具有一些字母,然后是2位数字:2位(您明白了)

是否有更好的方法安全地从所有这些字符串中提取可变日期(例如13 JUN 13:2223 APR 05:00)?

使用正则表达式可能吗?

apitx 回答:如何在字符串中找到这种格式的日期?

下面是一个正则表达式,它将与以下内容匹配...

[\d]{2} - matches exactly 2 digits
\s - matches single space
[\w]{3} - matches exactly 3 word characters
[\d]{1,2} - matches 1 or 2 digits,no more than 2,but at least 1

下面是一个有效的示例...

$str = 'This is some 734 random 6272834 data 04 FEB 1:01 and maybe some here';

preg_match('#[\d]{2}\s[\w]{3}\s[\d]{1,2}:[\d]{2}#',$str,$matches);

print_r($matches);
本文链接:https://www.f2er.com/3148536.html

大家都在问