2012-05-05 8 views
1

リンクはかなり関連していますが、私がそれをどのようにしたいのかはわかりません。私は正規表現を否定されたオープンタグとクローズタグに一致させたい。たとえば、この文字列を取る:Regex否定閉じる/開くタグ

<p>This <em>is</em> <span>a</span> <b>sentence</b>.</p> 

は私が一人で<p><span>を残したまま<em><b>にマッチする正規表現を使用します。私はこの次の正規表現を使用してください:

<(?!p|span)[^>]*> 

問題があり、上記</p></span>と一致します。私はこれらの終了タグを残しておきたい。私は試しました:

と私は何も試してみましたが、何も試してみました。私は助けを得ることができます願っています。どのように正規表現をと一致させるように設定することができますか?のようなものを実行すると、は実行されません。

+1

あなたが達成しようとしている結果とは何ですか? –

+0

私は承認していないhtmlタグを削除しようとしています。 – Err

答えて

3

これを試してください:

(?:<(em|b)[^<>]*?>)([^<>]+)(?=</\1>) 

説明:

<!-- 
(?:<(em|b)[^<>]*?>)([^<>]+)(?=</\1>) 

Options: case insensitive;^and $ match at line breaks 

Match the regular expression below «(?:<(em|b)[^<>]*?>)» 
    Match the character “<” literally «<» 
    Match the regular expression below and capture its match into backreference number 1 «(em|b)» 
     Match either the regular expression below (attempting the next alternative only if this one fails) «em» 
     Match the characters “em” literally «em» 
     Or match regular expression number 2 below (the entire group fails if this one fails to match) «b» 
     Match the character “b” literally «b» 
    Match a single character NOT present in the list “<>” «[^<>]*?» 
     Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
    Match the character “>” literally «>» 
Match the regular expression below and capture its match into backreference number 2 «([^<>]+)» 
    Match a single character NOT present in the list “<>” «[^<>]+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=</\1>)» 
    Match the characters “</” literally «</» 
    Match the same text as most recently matched by capturing group number 1 «\1» 
    Match the character “>” literally «>» 
--> 

このパターンは、開閉ペアでデータをタグ付けし、一致全体のためのものです。

しかし、あなたはタグのみを削除したい場合は、使用することができます:

</?(em|b)[^<>]*?> 

+0

ネストされている場合はどうなりますか? – ijse

関連する問題