C#Regex行为不符合预期

时间:2015-09-27 21:34:05

标签: c# html .net regex

我有一个正则表达式,它扫描HTML字符串并删除所有标记但留下内部文本内容。正则表达式如下:

<([^>"]*|"[^"]*")*>

我已经在http://regexr.com/上彻底测试了它,它的行为方式与我想要的一样。但是当我使用

string ret = Regex.Replace(HTML, @"<([^>""]*|""[^""]*"")*>", "");

我仍然在我的字符串中获得href标签。我不确定C#和regexr.com中的正则表达式之间的区别是什么。

P.S不,我不能使用HtmlAgilityPack,因为当嵌套的HTML元素太多时它会堆栈溢出,它经常发生。

测试示例: `

<description>
<!-- SC_OFF --><div class="md"><p>I was reading this comment threat about the upcoming Martian announcement. <a href="https://www.reddit.com/r/space/comments/3mjrdt/nasa_to_confirm_active_briny_water_flows_on_mars/cvfq50q">This comment</a> got me wondering.</p> <p>If you were in a decompression chamber and gradually decompressed (to avoid the bends), could you walk out onto the Martian surface with just an oxygen tank, provided that the surface was experiencing those balmy summer temperatures mentioned in the comment?</p> <p>I read The Martian recently, and I was thinking this possibility could have changed the whole book.</p> </div><!-- SC_ON --> submitted by <a href="https://www.reddit.com/user/jackwreid"> jackwreid </a> to <a href="https://www.reddit.com/r/askscience/"> askscience</a> <br/> <a href="https://www.reddit.com/r/askscience/comments/3mkson/given_time_to_decompress_slowly_could_a_human/">[link]</a> <a href="https://www.reddit.com/r/askscience/comments/3mkson/given_time_to_decompress_slowly_could_a_human/">[449 comments]</a>
</description>`

regexr.com正确识别非内部html,但我的C#给了我这个,没有删除正确的元素:

`

Given time to decompress slowly, could a human
survive in a Martian summer with just a oxygen mask?asksciencehttps://www.reddit
.com/r/askscience/comments/3mkson/given_time_to_decompress_slowly_could_a_human/
https://www.reddit.com/r/askscience/comments/3mkson/given_time_to_decompress_slo
wly_could_a_human/Sun, 27 Sep 2015 14:02:23 +0000&lt;!-- SC_OFF --&gt;&lt;div cl
ass=&#34;md&#34;&gt;&lt;p&gt;I was reading this comment threat about the upcomin
g Martian announcement. &lt;a href=&#34;https://www.reddit.com/r/space/comments/
3mjrdt/nasa_to_confirm_active_briny_water_flows_on_mars/cvfq50q&#34;&gt;This com
ment&lt;/a&gt; got me wondering.&lt;/p&gt; &lt;p&gt;If you were in a decompressi
on chamber and gradually decompressed (to avoid the bends), could you walk out o
nto the Martian surface with just an oxygen tank, provided that the surface was
experiencing those balmy summer temperatures mentioned in the comment?&lt;/p&gt;
 &lt;p&gt;I read The Martian recently, and I was thinking this possibility could
 have changed the whole book.&lt;/p&gt; &lt;/div&gt;&lt;!-- SC_ON --&gt; submitt
ed by &lt;a href=&#34;https://www.reddit.com/user/jackwreid&#34;&gt; jackwreid &
lt;/a&gt; to &lt;a href=&#34;https://www.reddit.com/r/askscience/&#34;&gt; asksc
ience&lt;/a&gt; &lt;br/&gt; &lt;a href=&#34;https://www.reddit.com/r/askscience/
comments/3mkson/given_time_to_decompress_slowly_could_a_human/&#34;&gt;[link]&lt
;/a&gt; &lt;a href="https://www.reddit.com/r/askscience/comments/3mkson/given_ti
me_to_decompress_slowly_could_a_human/"&gt;[449 comments]

`

0 个答案:

没有答案