説明
この正規表現は引用符で囲まれていない単語then
と一致します。
\bTHEN\b(?<!'\s*THEN(?=\s*'))
一部の言語は交代など\s?
や\s*
内部lookbehindsを許可していません。だから、あなたがそれらの言語の1つを使用しているなら、あなたはスペースをテストすることを賢明にする必要があります。
\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'''と一致します。私は、あなたが必要としないものをマッチングして、あなたが保つ必要があるものをマッチさせ、キャプチャすることを提案します。次に、言語固有の手段を使用して最終結果を取得します。 –