哈哈,你是不是也想
给你个参考
        private void getLists(string roomid, string pageid)
        {
            this.listView1.BeginUpdate();
            this.listView1.Items.Clear();
            ListViewItem item1;            string strUrl = "";
            if (roomid.Length < 4)
            {
                strUrl = "http://community.csdn.net/Expert/ForumsList.asp?roomid=" + roomid + "&typenum=1&xmlsrc=/Expert/deeptree/Rooms/List.xml&csdnstyle=0&hidetoc=true&whichpage=" + pageid;
            }
            else
            {
                strUrl = "http://community.csdn.net/Expert/ForumList.asp?roomid=" + roomid + "&typenum=1&xmlsrc=/Expert/deeptree/Rooms/List.xml&csdnstyle=0&hidetoc=true&whichpage=" + pageid;
            }            this.richTextBox1.Clear();
            this.richTextBox1.AppendText(strUrl + "\n\n");
            //string strReg = @"TopicView1.asp\?id=(?<id>[\s]*?)""[^>]*>(?<text>[\s]*?)</a>";
            //string strReg = @"TopicView1.asp\?id=(?<id>[\s\S]*?)""[^>]*>(?<text>[\s\S]*?)</a>";
            string strReg = @"TopicView1.asp\?id=(?<id>[\s\S]*?)""[^>]*>(?<text>[\s\S]*?)</a>[^<]*</td>[^<]*<td[^>]*>(?<t1>[\s\S]*?)</td>" +
                @"[^<]*<td[^>]*>(?<t2>[\s\S]*?)</td>[^<]*<td[^>]*>(?<t3>[\s\S]*?)</td>[^<]*<td[^>]*>(?<t4>[\s\S]*?)</td>";
            string getstr = "";
            int i = 0;
            string str = RDA.getIDLists(strUrl);
            //this.richTextBox1.AppendText(str + "\r\n \r\n \r\n \r\n ");
            Regex r = new Regex(strReg, RegexOptions.Compiled);
            for (Match m = r.Match(str); m.Success; m = m.NextMatch())
            {
                i++;
                this.richTextBox1.AppendText(i.ToString() + " ----------------------------------------------------------------------------------\r\n");
                getstr = m.Result("${id}");
                item1 = new ListViewItem(getstr, 0);
                this.richTextBox1.AppendText(getstr + "\r\n");
                getstr = m.Result("${text}");
                item1.SubItems.Add(getstr);
                this.richTextBox1.AppendText(getstr + "\r\n");
                getstr = m.Result("${t1}");
                item1.SubItems.Add(getstr);
                this.richTextBox1.AppendText(getstr + "\r\n");
                getstr = m.Result("${t2}");
                item1.SubItems.Add(getstr);
                this.richTextBox1.AppendText(getstr + "\r\n");
                getstr = m.Result("${t3}");
                item1.SubItems.Add(getstr);
                this.richTextBox1.AppendText(getstr + "\r\n");
                getstr = m.Result("${t4}");
                item1.SubItems.Add(getstr);
                this.richTextBox1.AppendText(getstr + "\r\n");
                listView1.Items.Add(item1);
            }
            this.listView1.EndUpdate();            this.richTextBox1.AppendText("\r\n\r\n");
            this.richTextBox1.AppendText(str);
        }

解决方案 »

  1.   

    sea026 兄:看得不是很明白啊可否给我完整点的例子?谢谢了。([email protected])
      

  2.   

    其实可以将它当XML来读取,不过上面书写得不是太规范,所以需要先调整:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xml As String = "<table> ... </table>"    '*** 调整没有用双引号括起来的属性
        Dim pattern As String = "=(?<value>[^""]\w*)"
        Dim replacement As String = "=""${value}"""
        Dim r As New System.Text.RegularExpressions.Regex(pattern)
        xml = r.Replace(xml, replacement)    '*** 将所有标记替换为大写
        pattern="<(?<>/)*(?<name>\S+)"
        xml = r.Replace(xml, pattern, _
            New System.Text.RegularExpressions.MatchEvaluator(AddressOf Me.MatchEvaluator))    '*** 读取这个XML
        Dim doc As New XmlDocument
        doc.LoadXml(xml)    Dim table As XmlNode = doc.FirstChild
        Me.DisplayNode(table, 0)
    End Sub'*** 执行替换的函数
    Private Function MatchEvaluator(ByVal m As System.Text.RegularExpressions.Match) As String
        Return m.Value.ToUpper
    End Function'*** 显示XMLNODE的所有属性,并通过递归显示其下的子NODE
    Private Sub DisplayNode(ByVal Node As XmlNode, ByVal Level As Integer)
        Dim s As String = New String(" "c, Level << 2)
        If TypeOf Node Is XmlText Then
            Debug.WriteLine(Node.Value)
        Else
            Debug.WriteLine(Node.Name)
        End If
        If Not Node.Attributes Is Nothing Then
            For Each a As XmlAttribute In Node.Attributes
                Debug.WriteLine(s & a.Name & "=" & a.Value)
            Next
        End If
        If Node.ChildNodes.Count > 0 Then
            For Each n As XmlNode In Node.ChildNodes
                Me.DisplayNode(n, Level + 1)
            Next
        End If
    End Sub以上不一定是个好方法,是在看你的帖子时忽然想到的,觉得挺有趣,就写出来了,希望能对你有帮助。
      

  3.   

    <TD width="14%"><FONT color=#631842>(?<content>[^<>]+)</FONT></TD>
      

  4.   

    Brunhild() 的方法有意思:)
    Sunmast(速马|ALL STAR) 的方法好
      

  5.   

    what kind of information do you want? if I understand you correctly,  Sunmast(速马|ALL STAR)'s method is good enough, for example:using System;
    using System.IO;
    using System.Text.RegularExpressions;class TestReg2
    {
     static void Main(string[] args)
     {
    string s = "........";

    Regex re = new Regex(@"<TD width=""14%""><FONT color=#631842>(?<content>[^<>]+)</FONT></TD>", RegexOptions.IgnoreCase);
    foreach (Match m in re.Matches(s))
    Console.WriteLine(m.Groups["content"].Value);
     }
    }if you want more flexibility, you could try
    using System;
    using System.IO;
    using System.Text.RegularExpressions;class TestReg2
    {
     static void Main(string[] args)
     {
    string s = "........";

    Regex re = new Regex(@"<table[^>]*>\s*<tr[^>]*>(\s*<TD[^>]*><FONT[^>]+>(?<content>[^<>]+)</FONT></TD>)*\s*</tr>\s*</table>", RegexOptions.IgnoreCase);
    Match m = re.Match(s);
    if (m.Success)
    {
    foreach (Capture c in m.Groups["content"].Captures)
    Console.WriteLine(c.Value);
    } }
    }
      

  6.   

    Function sss(ByVal ss As String)
            Dim sr As StreamReader = New StreamReader(ss, System.Text.Encoding.Default)
            Try
                Dim line As String
                Do
                    line = sr.ReadLine()
                    Dim i As Integer
                    If line.Length > 27 Then
                        For i = 1 To line.Length
                            Dim str As String
                            Try
                                If Mid(line, i, 7) = "631842>" Then
                                    Dim j As Integer
                                    For j = i + 7 To line.Length
                                        If Mid(line, j, 1) = "<" Then
                                            Exit For
                                        End If
                                        str = str & Mid(line, j, 1)
                                    Next
                                    Label1.Text = str
                                    Exit For
                                End If
                            Catch ex As Exception
                                Exit For
                            End Try
                        Next
                    End If
                Loop Until line Is Nothing
                sr.Close()
                Return "ok"
            Catch ex As Exception
                Return "err"
            End Try
        End Function