douzai8285 2016-02-08 15:14
浏览 24
已采纳

带有PHP preg_match_all的条件RgEx

Just have to select Conditional Regex.

Here is what i have problem....

I just want to select time string 02:00 but not with quotation marks like "02:00" or '02:00'

I am using to select 02:00 or 2:00 Regex is

$pattern = '/(\d{2}:\d{2})|(\d{1}:\d{2})/';
preg_match_all($pattern,$content, $matches); 

But is selecting this also "02:00" or '02:00' I am not sure which regex will use to skip that time string.

I got this http://www.regular-expressions.info/conditional.html But not sure how make regex :(

Please help me out.

UPDATE

Thanks for Lucas Trzesniewski and his Great Help.

I just resolved my issue with this php code.

 $pattern = '/(?!["\'])\b\d{1,2}:\d{2}\b(?!["\'])/';
  preg_match_all($pattern,$content, $matches); 
  • 写回答

1条回答 默认 最新

  • donglian8407 2016-02-08 15:16
    关注

    Conditionals won't help you much in this case.

    The simplest way is to use lookarounds:

    (?<!["'])\b\d{1,2}:\d{2}\b(?!["'])
    

    Demo

    • (?<!["']) (negative lookbehind) will make sure the preceding character is not single or double quote
    • (?!["']) (negative lookahead) will do the same for the following character
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失