0

我有一个这样的字符串

字符串文本 =

<p><span><span id="test">Meanwhile, the Cougars are coming off of a win against Eastern 
Washington University in which they scored 88 points and had three players score at least 
15 points. <span>Motum</span> recorded his fourth career double-double in the game as well.  
</span></span></p> 

<p><span>After Dexter Kernich-Drew, Royce Woolridge, and Will DiIorio were unable to 
practice last Wednesday before the game against EWU, the team is healthy and ready to play 
against Utah Valley. </span></p>


<p><span><span><span>Woolridge</span>, a <span>redshirt</span> sophomore transfer who has 
started at guard in the first two games this season, scored seven points and had two assists 
against EWU. He also had 10 points and three assists against Saint Martin&rsquo;s. </span> 
</span></p>

而且我需要摆脱所有没有属性而只是包装内容的 's。我到目前为止的模式是

text = Regex.Replace(text, @"</?span([^>]*|/)?>", "", RegexOptions.Compiled);

这只是拉出所有跨度离开

<p>Meanwhile, the Cougars are coming off of a win against Eastern Washington University 
in which they scored 88 points and had three players score at least 15 points. Motum 
recorded his fourth career double-double in the game as well. </p> 

<p>After Dexter Kernich-Drew, Royce Woolridge, and Will DiIorio were unable to practice 
last Wednesday before the game against EWU, the team is healthy and ready to play 
against Utah Valley. </p> 

<p>Woolridge, a redshirt sophomore transfer who has started at guard in the first 
two games this season, scored seven points and had two assists against EWU. He also had 
10 points and three assists against Saint Martin&rsquo;s. </p>

那很接近,但我需要第一个

它看起来像

<p><span id="test">Meanwhile, the Cougars are coming off of a win against Eastern 
Washington University in which they scored 88 points and had three players score at 
least 15 points. Motum recorded his fourth career double-double in the game as well. 
</span></p>

这里的问题是如何找到没有属性的嵌套跨度并删除它们。我确实有一些其他尝试使用回溯作为结束标记,但这是唯一一个最接近的。

4

2 回答 2

0
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var spans = doc.DocumentNode.SelectNodes("//span[@*]")
                .Select(s => s.InnerText)
                .ToList();
于 2012-11-15T20:50:28.987 回答
0

Here's some pseudocode for a simple algorithm:

create a stack of booleans

set the last position to the start of the text

search for the opening and the closing spans and for each one found:
    append the text since the last position up to the start of the found item to the output

    if the found item is an opening span:
        if the found item has attributes:
            // it's an opening span with attributes
            // we want to keep it
            push true onto the stack
            append the item to the output
        else:
            // it's an opening span without attributes
            // we want to drop it
            push false onto the stack
    else:
        pop the top boolean from the stack
        if the popped boolean is true:
            // the corresponding opening span had attributes
            // we want to keep this closing span
            append the found item to the output

    set the last position to the end of the found item

append the remaining text since the last position to the output
于 2012-11-15T21:09:18.497 回答