C#:使用regex Grouping Constructs删除捕获组以外的标记

时间:2013-10-17 06:05:39

标签: c# regex regex-negation regex-lookarounds

我想删除除某些分组标记以外的所有标记。我有一个包含更多标签的文本。我只想删除除了某些标签之外的所有标签。只是一个简单的例子

<first>This <em>is</em> an <strong>testing</strong> data</first><second> testing <b>data</b> second</second><third>testing <i>data</i> third </third>

我只想删除“em,i,b”以外的所有标签。输出文字如下

This <em>is</em> an <strong>testing</strong> data testing <b>data</b> second<testing <i>data</i> third 

如何在正则表达式中执行此操作。

我试过以下

sampleStr = Regex.Replace(sampleStr , "<(?!(strong|em|u|i|b))>", "");

但它不起作用..

1 个答案:

答案 0 :(得分:4)

你非常接近:

<(?!/?(?:strong|em|i|u|b))[^>]+>

这是一个“正式”的解释:

Remove Other Tags

<(?!/?(?:strong|em|i|u|b))[^>]+>

Match the character “<” literally «<»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!/?(?:strong|em|i|u|b))»
   Match the character “/” literally «/?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match the regular expression below «(?:strong|em|i|u|b)»
      Match either the regular expression below (attempting the next alternative only if this one fails) «strong»
         Match the characters “strong” literally «strong»
      Or match regular expression number 2 below (attempting the next alternative only if this one fails) «em»
         Match the characters “em” literally «em»
      Or match regular expression number 3 below (attempting the next alternative only if this one fails) «i»
         Match the character “i” literally «i»
      Or match regular expression number 4 below (attempting the next alternative only if this one fails) «u»
         Match the character “u” literally «u»
      Or match regular expression number 5 below (the entire group fails if this one fails to match) «b»
         Match the character “b” literally «b»
Match any character that is NOT a “>” «[^>]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “>” literally «>»


Created with RegexBuddy