doupa1883 2016-04-13 08:46
浏览 212

PHP preg_match正则表达式,用于查找字符串中的日期

I try to make system that can detect date in some string, here is the code :

$string = "02/04/16   10:08:42";
$pattern = "/\<(0?[1-9]|[12][0-9]|3[01])\/\.- \/\.- \d{2}\>/";
$found = preg_match($pattern, $string);
            if ($found) {
                echo ('The pattern matches the string');
            } else {
                echo ('No match');
            }

The result i found is "No Match", i don't think that i used correct regex for the pattern. Can somebody tell me what i must to do to fix this code

  • 写回答

2条回答 默认 最新

  • dtz8044 2016-04-13 09:48
    关注

    First of all, remove all gibberish from the pattern. This is the part you'll need to work on:

    (/0?[1-9]|[12][0-9]|3[01]/)
    

    (As you said, you need the date only, not the datetime).

    The main problem with the pattern, that you are using the logical OR operators (|) at the delimiters. If the delimiters are slashes, then you need to replace the tube characters with escaped slashes (/). Note that you need to escape them, because the parser will not take them as control characters. Like this: \/.

    Now, you need to solve some logical tasks here, to match the numbers correctly and you're good to go. (I'm not gonna solve the homework for you :) )

    These articles will help you to solve the problem tough:

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致