1

我在阅读每一行以计算正则<a href= 表达式匹配时的正则表达式时遇到问题,它将跳过该行并且直到最后才完成。例如,这里我有两个<a href标签,但是在阅读第一个标签后它会跳转到下一行:

"<p class="txt">Everyone seemed cool with that, including Coach A, so we went to work putting it all together. The end result had us starting off standing in a <strong>V formation</strong><a href="../Text/chap5.html#b1" id="f1">*</a>, and then the girls in back would tumble forward so that we were all in a straight line for the cheer. It was a really cool idea! To finish things off, we’d transition into two stunt groups that would do side-by-side <strong>elevators</strong><a href="../Text/chap5.html#b2" id="f2"></a>.</p>"

这是我的代码:

        Dim count1 As Integer = 0
        For p As Integer = 0 To lvw.Items.Count - 1
        Dim r As StreamReader = New StreamReader(New FileStream(Path.Combine(lvw.Items(p).Tag, lvw.Items(p).Text), FileMode.Open, FileAccess.Read, FileShare.Read))

        Do While Not r.EndOfStream
            Dim strr1 As String = r.ReadLine.Trim
            Try
                Dim regex As Regex = New Regex("<a\s+[^>]*(href\s*=\s*(['""]).*?\2)")
                Dim m As Match = regex.Match(strr1)

                If m.Success Then
                    count1 += 1

                End If
                Label2.Text = (count1.ToString)
            Catch ex As Exception
            End Try
        Loop

    Next
4

1 回答 1

0

你需要使用ReadLine吗?您可以使用ReadToEnd一次获取整个文本,然后使用

regex.Matches(strr1).Count

获取匹配数。

于 2012-10-19T04:52:14.423 回答