用C# 正则 提取HTML标签中的值?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = @"<tr><td font=red>2009-1-2</td><td font=red>200</td><td>110</td></tr>

<tr><td>2009-1-3</td><td>200</td><td>110</td></tr>

<tr><td font=blue>2009-1-4</td><td>200</td><td>110</td></tr>";

            string st = RegStr(s);
            Console.WriteLine(st);
            Console.ReadLine();

        }

        public static string RegStr(string objStr)
        {
            string returnStr = "";
            MatchCollection ml = Regex.Matches(objStr, "<tr>.*?</tr>");
            foreach (Match m in ml)
            {
                string str = m.Value.Replace("</td></tr>", ",");
                str = str.Replace("</td>", "|");
                str = Regex.Replace(str, "<.*?>", "");
                returnStr += str;
            }
            return returnStr;
        }
    }
}

posted @ 2010-05-17 16:03  左少白  阅读(4908)  评论(1编辑  收藏  举报