现在一段HTML的字符串如下:
<P STYLE=\"margin:0 0 0 0;font-family:宋?体??;font-size:13.3333333333333;\"> <SPAN>c</SPAN><SPAN STYLE=\"text-decoration:underline;\">o</SPAN>
<SPAN>mmon</SPAN></P>
我现在主要从中提取
<SPAN STYLE=\"text-decoration:underline;\">o</SPAN>
出来,我暂用的正则表达式是
<SPAN.*(?=text-decoration:underline;)(.|\n)*?</SPAN>
如果上述HTML代码是
<P STYLE=\"margin:0 0 0 0;font-family:宋?体??;font-size:13.3333333333333;\"> 
<SPAN>c</SPAN>
<SPAN STYLE=\"text-decoration:underline;\">o</SPAN>
<SPAN>mmon</SPAN></P>
分行来显示,则可以满足我的效果。但如果是HTML只放在一个字符串中时,则无法获取,求如何获取
<SPAN STYLE=\"text-decoration:underline;\">o</SPAN>的正则表达式书写方法?

解决方案 »

  1.   

    补充一句,用<SPAN.*(?=text-decoration:underline;)(.|\n)*?</SPAN>
    正则表达式的话,出现在结果是
    <SPAN>c</SPAN><SPAN STYLE=\"text-decoration:underline;\">o</SPAN>它把前面不要部分<SPAN>c</SPAN>也加了进来,这就不是我要的效果了。
      

  2.   

    <SPAN[^>]*(?=text-decoration:underline;)(.|\n)*?</SPAN>
    在你原来的基础上改的
      

  3.   


    void Main()
    {
       string html="<P STYLE=\"margin:0 0 0 0;font-family:宋?体??;font-size:13.3333333333333;\"> <SPAN>c</SPAN><SPAN STYLE=\"text-decoration:underline;\">o</SPAN><SPAN>mmon</SPAN></P>"; foreach(Match m in  Regex.Matches(html,@"(?is)<span[^>]*style=(['""]?)text-decoration:underline;\1>.*?</span>"))
    {
      Console.WriteLine(m.Value);
    }
    }/*
    <SPAN STYLE="text-decoration:underline;">o</SPAN>
    */
      

  4.   

    try...Regex reg = new Regex(@"(?is)<SPAN[^>]*?STYLE=""text-decoration:underline;""[^>]*>>.*?</SPAN>");
      

  5.   

    4楼多了个字符            Regex reg = new Regex(@"(?is)<SPAN[^>]*?STYLE=""text-decoration:underline;""[^>]*>.*?</SPAN>");
                MatchCollection mc = reg.Matches(yourStr);
                foreach (Match m in mc)
                {
                    richTextBox2.Text += m.Value + "\n";
                }