简体   繁体   中英

Visual Basic Multiple Regex inserting into DataGridView

Below is part of my code for a small webscraping project I'm working on. I'm having an issue inserting the values of the second Regex into the datagrid and have them line up with the values of the first regex. Each page will have a list of ten items, each with a unique ID along with a format and a rating. When the code is run, it only adds the formatID data to the first ten rows.

  Dim r As New System.Text.RegularExpressions.Regex("<div id=""R.*"" class=""a-section review"">")
        Dim Matches As MatchCollection = r.Matches(SourceCode)
        For Each ItemID As Match In Matches

            DataGridView1.Rows.Add("", Split(ItemID.Value, """").GetValue(1), AsinTextBox.Text, "", "", "", "")

        Next


        Dim R2 As New System.Text.RegularExpressions.Regex("<a class=""a-size-mini a-link-normal a-color-secondary"" href=""/.*/product-reviews/.*/ref=.*"">.*</a>")
        Dim Matches2 As MatchCollection = R2.Matches(SourceCode)
        Dim Z2
        Dim i As Integer = 0
            For Each FormatID As Match In Matches2

                i = i + 1
                Z2 = Split(FormatID.Value, ">").GetValue(1)
                Z2 = Split(Z2, "<").GetValue(0)
                DataGridView1.Rows(i).Cells(5).Value = (Z2)

            Next

I figured it out. Instead of doing multiple Regex searches, I did one larger one that incorporated all of the three that I was trying to do previously. The three regex searches from before were all apart of one large string of html and after I had searched for it I used the split function to get the correct value for Z2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM