只有匹配时,如果两者都是负向前瞻和Lookbehind失败

问题描述:

我试图创建一个正则表达式来检查句子中是否存在单词,如果它没有用单引号括起来。只有匹配时,如果两者都是负向前瞻和Lookbehind失败

我已经尝试了不同的正则表达式,如:

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

比赛 '那么' | '那么',这不应该匹配。

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

匹配“然后”,这不应该匹配

我真的很坚持,因为我不知道哪个正则表达式的作品。我也试过其他正则表达式,但它不匹配:

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

一些输入将不胜感激!

谢谢!

+0

这个词是引号内唯一一个单词还是引用句子的一部分? – ClasG

+0

@ClasG只有当单词是引号内的唯一字。 –

+0

您的要求不太清楚。尝试'(?i)THEN(?

说明

此正则表达式匹配未用引号括起来的字then

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

Regular expression visualization

一些语言不允许交替如\s?\s*内lookbehinds。所以,如果你使用其中的一种语言,那么你需要获得关于测试空间的信息。

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