日期:2014-05-18  浏览次数:21019 次

C# 如何截取一段内容,不要样式表,标点符号也要获取的
内容如下:
  <span style="border:1px solid red">江,苏<font color="red" size="5">省、南</font>京市</span>
  获取后得到的是:江,苏省、南京市  

  这样的 

C# code

        string sourcestr = "<span style="border:1px solid red">江,苏<font color="red" size="5">省、南</font>京市</span>
"
        Regex regx = new Regex(@"[\u4e00-\u9fa5]+");
        List<string> urllist = new List<string>();
        string emp = "";
        MatchCollection mcurl = regx.Matches(sourcestr);
        foreach (Match sk in mcurl)
        {
            emp += sk.Groups[0].Value
        }



这段代码只能获取到 江苏省南京市

求解:

------解决方案--------------------
C# code
public static string ClearMarket(string oldstring) {
            string str  = Regex.Replace(oldstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            str.Replace("<", "");
            str.Replace(">", "");
            str.Replace("\r\n", "");
            str = HttpContext.Current.Server.HtmlEncode(str).Trim();
            return str;
        }

------解决方案--------------------
str=Regex.Replace(str,"<[^<>]+?>","");
------解决方案--------------------
一段vb的
Function replHTML(strng) 
Dim reg 
Set reg = new RegExp 
reg.Pattern = " <[^> ]+> " 
reg.Global = True 
replHTML = reg.Replace(strng, " ") 
Set reg = Nothing 
End Function

改改就可以用,思想是去掉所有的<[^> ]+>,也就是html标签