2016-06-14 32 views
2

我試圖創建一個正則表達式來檢查句子中是否存在單詞,如果它沒有用單引號括起來。只有匹配時,如果兩者都是負向前瞻和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(?='))'這與'then'不匹配,但會匹配'then'和'then''。我會建議匹配你不需要和匹配,並捕捉你需要保留的東西。然後用語言特定的手段來獲得最終結果。 –

回答

0

說明

此正則表達式匹配未用引號括起來的字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 
----------------------------------------------------------------------