【问题标题】:How to not include in regex a literal after including it in the first group?如何在将文字包含在第一组中后不包含在正则表达式中?
【发布时间】:2015-04-06 16:59:50
【问题描述】:

我正在使用 preg_match_all() 来匹配一些字符串。

这是我的代码

preg_match_all('@([a-zA-Z0-9,\(\)\-\s\.\#:]*)Date From: [0-9]{2}/[0-9]{2}/[0-9]{4}([, \s]*[0-9]{2}/[0-9]{2}/[0-9]{4})*\s(Division Type: [A-Z ]*)*@m', $string, $match);

这是输出

https://regex101.com/r/hE3iO2/1

现在,我想在第一个组中添加一个 / 以在捕获组中包含文字 /,这会发生

从这里

([a-zA-Z0-9,\(\)\-\s\.\#:]*)

到这里

([a-zA-Z0-9,\(\)\-\s\.\#:/]*)

https://regex101.com/r/wM1rW7/1

将/ 添加到组时如何防止这种情况发生?

【问题讨论】:

    标签: php regex preg-match-all capturing-group


    【解决方案1】:

    让你的正则表达式不贪婪:

    ([a-zA-Z0-9,()\s.#:/-]*?)Date From: [0-9]{2}/[0-9]{2}/[0-9]{4}([, \s]*[0-9]{2}/[0-9]{2}/[0-9]{4})*\s(Division Type: [A-Z ]*)*
    

    RegEx Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多