在 c# 上使用 spases 的正则表达式负后视

Regex Negative Lookbehind with spases on c#

我无法理解正则表达式负向回顾..

如果前面有一个 'not' 单词,我不需要捕捉 '(this)' 一些空格。 例如:

  1. ...(这个)-赶上
  2. ... 不是(这个) - 没有赶上
  3. ... not (this) - 没抓到,但确实抓到了

请告诉我哪里错了,我做不到。 我的模板:

(?<!\bnot\b)\s*(\(.*?this.*?\))

(?<!...)无法理解(?<!\bnot\b\s*)

之类的东西

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

嗯,您使用的是错误的在线正则表达式测试器,您需要一个支持 .NET 正则表达式语法的工具。 Regex101.com 不支持 .NET 正则表达式语法。

你可能真的会用到

(?<!\bnot\b\s*)\(this\)

this regex demo

图案解释:

  • (?<!\bnot\b\s*) - 如果有
    • \b - 前导词边界
    • not - not 文字子串
    • \b - 尾随单词边界
    • \s* - 零个或多个空格
  • \( - 文字 ( 符号
  • this - 文字字符串 this
  • \) - 文字 ) 符号。

请注意,此模式与 rtjtj bbg (this,and that) 中的 this 不匹配。要使其匹配 this,您可以在最后一个 \) -> (?<!\bnot\b\s*)\(this\)? 之后添加一个 ?(一或零)量词。您可以进一步调整图案。