日期:2014-05-17  浏览次数:20808 次

C#返回正则表达式全部匹配结果代码
C#返回正则表达式全部匹配结果,并填进Textbox里,代码应该怎样写的?
在Google搜索的,运行时会导致机器假死。
------解决方案--------------------
MatchCollection mc= Regex.Matches(str, @" <a[^> ]*href=([ ' " "]?)(? <url> [^ ' " "> \s]*)\1?[^> ]*> (? <text> [^ <]*) </a> ", RegexOptions.IgnoreCase);   
foreach (Match m in mc)   
{   
  textbox1.text+=m.Groups[ "url "].Value;     
}   

------解决方案--------------------
你的正则中根本就没有名称为name的命名捕获组,m.Groups["name"].Value;当然是空白

你的代码中有很多用不到的废代码

        public class ExtHtml
        {
            public void Htmlx(string srclist, TextBox rstlist)
            {
                MatchCollection mc = Regex.Matches(srclist, "(?<=<span[^>]*?class=\"list_row_0\">)\\w+", RegexOptions.IgnoreCase);
                foreach (Match m in mc)
                {
                    rstlist.Text += m.Value;
                }
            }
        }