Counting Matches

Abev107

New member
Joined
May 11, 2007
Messages
1
I have code that performs an html scrape:
Code:
Dim myConnection As New SqlConnection("server=(local);database=xxxx;Trusted_Connection=yes")
            
        Dim strAddress As String = ("http:" & straddress & ".html")
        
        Dim oRequest As WebRequest = WebRequest.Create(strAddress)
        Dim oResponse As WebResponse = oRequest.GetResponse()

        Dim oStream As Stream = oResponse.GetResponseStream()
        Dim oStreamReader As New StreamReader(oStream, Encoding.UTF8)

        Dim strData As String = oStreamReader.ReadToEnd()
              
        Dim regGames As New Regex("[regexpattern]", RegexOptions.Singleline)
        
        Dim mGames As Match = regGames.Match(strData)
        ***************************************
    While mGames.Success
        Dim writeGames As New SqlCommand( _
        "INSERT [insert statement]", myConnection)
                
        writeGames.Connection.Open()
        writeGames.ExecuteNonQuery()
        writeGames.Connection.Close()
                         
        Dim strGame As String = mGames.Groups(1).Value & " " & _
        mGames.Groups(2).Value & " @ "
            
        next game
        mGames = mGames.NextMatch
            
        strGame = strGame & mGames.Groups(1).Value & " " & _
        mGames.Groups(2).Value()
            
        print
        Response.Write(strGame & "<br>")
            
        next set and loop
        mGames = mGames.NextMatch
End While

What I need to do is once I apply the regex to the scrape, I want to get the total number of elements (games) before I do any further processing.

AFAIK there is no way to get a "count" of the number of matches from the "Match" object, but there is with matchcollection?

Could someone tell me if there is a way to get a count from the regex before I do any processing?
 
Matches vs Match

Im not sure how you expect to count the number of matches before actually performing the matching process. As you rightly point out the MatchCollection object does expose a count, so this raises the question of why youre not using it. If you use the Matches method, rather than Match, you can collect all the matches at once and then loop through them:

Code:
Dim mAllGames As MatchCollection = regGames.Matches(strData)

Response.Write("There are " + mAllGames.Count + " games")

For Each mGame As Match In mAllGames
    Process mGame here
Next

I would also suggest you open the database connection before entering the loop, and close it after the loop, rather than opening and closing on each iteration.

Good luck :)
 

Similar threads

I
Replies
0
Views
146
Innovators World Wide
I
M
Replies
0
Views
83
mipakteh
M
M
Replies
0
Views
150
mipakteh
M
Back
Top