doushouj966020 2016-01-27 09:46
浏览 58
已采纳

如何使用Regex匹配字符串中的PHP time()或microtime()?

I currently have a regex command which matches php time in a string:

preg_match( '/([a-z]+)_([0-9]{9,})\.jpg/i', $aName, $lMatches );

How can I modify this to also match microtime() in the same match?

Examples:

foobar_1453887550.jpg (match)

foobar_1453887620.8717.jpg (match)

foobar_123.jpg (don't match)

foobar_adsf123123.jpg (don't match)

  • 写回答

2条回答 默认 最新

  • douxiandiyo58855 2016-01-27 09:52
    关注

    Add optional group using ?:

    preg_match( '/([a-z]+)_([0-9]{9,})(\.[0-9]{4,})?\.jpg/i', $aName, $lMatches );
    

    Here (\.[0-9]{4,})? is an optional group which can present or not in your string.

    Considering @trincot remark you can change optional group to (\.[0-9]+)? if ending zeroes will not present in milliseconds.

    preg_match( '/([a-z]+)_([0-9]{9,})(\.[0-9]+)?\.jpg/i', $aName, $lMatches ); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试