【问题标题】:Skip match if in specific content如果在特定内容中则跳过匹配
【发布时间】:2019-10-22 10:23:40
【问题描述】:

我需要匹配文本中的给定单词,除非在特定句子中找到。

到目前为止,我创建了以下正则表达式:

(?!^)\G(?:['"][a-z0-9:\/._-]+example.com[a-z0-9:\/._-]+['"])(*SKIP)(*FAIL)|((?:\b|[_])elementor)(?:\b|[_])

因此,如果在 'http://example.com/content/elementor' 中找到了 elementor 这个词,这不应该匹配。

这是一个正则表达式测试器https://regex101.com/r/NMOvfx/1/

【问题讨论】:

  • 可能是https?:\/\/[^\s'"]*(*SKIP)(*FAIL)|(?<![^\W_])(elementor)(?![^\W_])?
  • @WiktorStribiżew 这似乎工作得很好!您可以发布您的解决方案以便我接受吗?
  • 完成,见下文。
  • @WiktorStribiżew 顺便问一下,有没有办法在模式中添加第二个 (*SKIP)(*FAIL) 标准?例如所以它忽略 if in sentence font-family: regex101.com/r/7M9nce/3
  • 重点是使用组:(?:exclude1|exclude2)(*SKIP)(*F)|match_me_instead

标签: php regex


【解决方案1】:

你可以使用

'~https?://[^\s'"]*(*SKIP)(*FAIL)|(?<![^\W_])(elementor)(?![^\W_])~'

regex demo

详情

  • https?:// - https://http://
  • [^\s'"]* - 0 个或多个除空格以外的字符,'"
  • (*SKIP)(*FAIL) - 当前匹配被丢弃,从失败的匹配端开始搜索下一个匹配
  • | - 或
  • (?&lt;![^\W_]) - 前面没有单词的位置或_ char
  • (elementor) - 第 1 组:单词 elementor
  • (?![^\W_]) - 后面没有单词或 _ 字符的位置。

【讨论】:

  • 感谢维基人!!拯救了我的一天??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
相关资源
最近更新 更多