1

関連するかなりの数のリンクが見つかりましたが、私が望む方法の領域には何もありません。否定された開始タグと終了タグに一致する正規表現が必要です。たとえば、次の文字列を使用します。

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

正規表現を使用して and を照合<em>し、 andだけ<b>を残します。これには、次の正規表現を使用します。<p><span>

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

問題は、上記が と に一致すること</p>です</span>。それらの終了タグも残したいです。私はもう試した:

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

およびそれのさまざまな組み合わせですが、私が試したことはありません。助けが得られることを願っています。次のようなことをせずに、これらに一致するように正規表現を設定するにはどうすればよいですか: <(?!p|span)[^>]*>(.*?)</(?!p|span)[^>]*>(見栄えが悪く、おそらくより多くのリソースが必要です)。

4

1 に答える 1

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 “&lt;” 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 “&lt;>” «[^<>]*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match the character “&gt;” literally «>»
Match the regular expression below and capture its match into backreference number 2 «([^<>]+)»
   Match a single character NOT present in the list “&lt;>” «[^<>]+»
      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 “&lt;/” literally «</»
   Match the same text as most recently matched by capturing group number 1 «\1»
   Match the character “&gt;” literally «>»
-->

このパターンは、タグ付けされたデータ全体を開始と終了のペアと一致させるためのものです。

ただし、タグのみを削除したい場合は、次を使用できます。

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

于 2012-05-05T11:18:22.573 に答える