2016-06-14 8 views
2

私は一重引用符で囲まれていない場合にのみ、単語が文内に存在するかどうかを確認する正規表現を作成しようとしています。 | 'し、'ネガティブルックアヘッドとルックビハインドの両方が失敗した場合のみ一致します

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

試合:

は、私は、次のようなさまざまな正規表現を試してみました'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を許可していません。だから、あなたがそれらの言語の1つを使用しているなら、あなたはスペースをテストすることを賢明にする必要があります。

\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 
---------------------------------------------------------------------- 
関連する問題