gpt4 book ai didi

regex - 仅当 Negative Lookahead 和 Lookbehind 都失败时匹配

转载 作者:行者123 更新时间:2023-12-02 03:14:37 34 4
gpt4 key购买 nike

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

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

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

匹配'然后' | ' then',不应该匹配。
(?<!' )(?i:THEN)(?! ') or (?<!(' ))(?i:THEN)|(?i:THEN)(?!( '))

匹配不应该匹配的“then”

我真的很困惑,因为我不知道哪个 Regex 有效。我也试过其他正则表达式,但它不匹配:
' then I jumped.
He said then 'Wow'.

一些意见将不胜感激!

谢谢!

最佳答案

描述

此正则表达式将匹配单词 then没有被引号包围。

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

Regular expression visualization

某些语言不允许交替,例如 \s?\s*内部回顾。因此,如果您正在使用其中一种语言,那么您需要更清楚地测试空格。

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

Regular expression visualization

例子

现场演示

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
----------------------------------------------------------------------

关于regex - 仅当 Negative Lookahead 和 Lookbehind 都失败时匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811360/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com