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>

そして、属性がなく、コンテンツをラップしているだけのすべてのものを取り除く必要があります。私が今まで持っているパターンは

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

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 に答える
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 に答える