【问题标题】:regex - Exclude the line with the word (php)正则表达式 - 排除带有单词的行 (php)
【发布时间】:2020-07-29 15:51:43
【问题描述】:

请帮忙,如果字符串中存在某个单词,如何排除字符串的存在。

例子:

any text here my text any text word wrong any text
any text here my text any text true any text
any text here my text any text wrong any text

此正则表达式标记所有行 (https://regex101.com/r/rrgEp6/1):

here([a-z, ]{0,70})((?:(?! word))) (wrong|true) 

在这种情况下需要排除单词-“单词”:

any text here my text any text true any text
any text here my text any text wrong any text

【问题讨论】:

  • 你想要here((?:(?!\bword\b)[a-z ,]){0,70}) (wrong|true) ,见demo。

标签: php regex


【解决方案1】:

你可以使用

here((?:(?!\bword\b)[a-z ,]){0,70}) (wrong|true)

见regex demo

详情

  • here - 一个字符串
  • ((?:(?!\bword\b)[a-z ,]){0,70}) - 第 1 组:小写字母、空格或逗号,出现 0 到 70 次,不以整个单词字符序列开头的 word
  • - 空格
  • (wrong|true) - 第 2 组:wrong 或 true 字符序列。

【讨论】:

    【解决方案2】:

    你可以使用

    ^(?!.*\bword\b).+
    

    见a demo on regex101.com。

    【讨论】:

      猜你喜欢
      • 2016-05-09
      • 2019-04-30
      • 2013-02-28
      • 2021-04-20
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      相关资源
      最近更新 更多