說明
此正則表達式匹配未用引號括起來的字then
。
\bTHEN\b(?<!'\s*THEN(?=\s*'))
一些語言不允許交替如\s?
或\s*
內lookbehinds。所以,如果你使用其中的一種語言,那麼你需要獲得關於測試空間的信息。
\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
----------------------------------------------------------------------
這個詞是引號內唯一一個單詞還是引用句子的一部分? – ClasG
@ClasG只有當單詞是引號內的唯一字。 –
您的要求不太清楚。嘗試'(?i)THEN(?<!'THEN(?='))'這與'then'不匹配,但會匹配'then'和'then''。我會建議匹配你不需要和匹配,並捕捉你需要保留的東西。然後用語言特定的手段來獲得最終結果。 –