【问题标题】:Only Match if BOTH Negative Lookahead and Lookbehind fails仅当 Negative Lookahead 和 Lookbehind 都失败时才匹配
【发布时间】:2016-06-14 11:58:27
【问题描述】:

我正在尝试创建一个正则表达式来检查一个单词是否存在于一个句子中,当且仅当它没有被单引号括起来。

我尝试过不同的正则表达式,例如:

(?<!' )(?i:THEN)|(?i:THEN)(?! ')

匹配 'then' | 'then',不应该匹配。

(?<!' )(?i:THEN)(?! ') or (?<!(' ))(?i:THEN)|(?i:THEN)(?!( '))

匹配不应该匹配的'then'

我真的很困惑,因为我不知道哪个 Regex 有效。我也尝试过其他正则表达式,但它无法匹配:

' then I jumped.
He said then 'Wow'.

我们将不胜感激!

谢谢!

【问题讨论】:

  • 是当单词是引号中唯一的单词,还是当它也是引用句子的一部分时?
  • @ClasG 仅当单词是引号内的唯一单词时。
  • 您的要求不是很清楚。试试(?i)THEN(?&lt;!' THEN(?= ')) 这不会匹配' then ',但会匹配' then''then '。我建议匹配您不需要的内容并匹配并捕获您需要保留的内容。然后使用特定语言的方式来获得最终结果。
  • 您能否澄清一下,您使用的是哪个正则表达式引擎?可能的解决方案可能取决于这一事实。

标签: regex


【解决方案1】:

说明

这个正则表达式将匹配没有被引号包围的单词then

\bTHEN\b(?<!'\s*THEN(?=\s*'))

某些语言不允许在lookbehinds 中进行交替,例如\s?\s*。因此,如果您使用的是其中一种语言,那么您需要对空格测试有所了解。

\bTHEN\b(?<!'\sTHEN(?=\s'))(?<!'THEN(?='))

示例

现场演示

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

then             matched
'then            matched
then'            matched
'then'
' then           matched
then '           matched
' then '
' then I jumped.       matched
He said then 'Wow'.    matched
SthenS

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  THEN                     'THEN'
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
    THEN                     'THEN'
----------------------------------------------------------------------
    (?=                      look ahead to see if there is:
----------------------------------------------------------------------
      \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
----------------------------------------------------------------------
    'THEN                    '\'THEN'
----------------------------------------------------------------------
    (?=                      look ahead to see if there is:
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2015-09-22
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多