Hello, so i'm having problem with Regex. I'm trying to use to put names in a checkboxlist. But the page where i want the names from have to be accesed by login in on the website. But the regex doesnt read from the VB WebBrowser it reads from... the internet(?) SO long story Short "How can i make A regex find the names from The VB Webbrowser, So i can log in on the VB WebBrowser and navigate to the page to find the names. Excuse me if i'm bad at explaining.

Also here is my general Regex code i dont know if it's suitable, as it hasn't really worked for places other than where it was used in the Video Tutorial i saw (...Runescape.) I don't play runescape By the way.

Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://services.runescape.com/m=itemdb_rs/frontpage.ws")
        Dim response As System.Net.HttpWebResponse = request.GetResponse
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())

        Dim rssourcecode As String = sr.ReadToEnd

        Dim r As New System.Text.RegularExpressions.Regex("<img name=""object"" src="""" alt="".*"">")
        Dim matches As MatchCollection = r.Matches(rssourcecode)

        For Each itemcode As Match In matches

            CheckedListBox1.Items.Add(itemcode.Value.Split("""").GetValue(5))

and ofcourse

Imports System.Text.RegularExpressions

at the very top.

Thank you in advance :)

Dani AI

Generated

The problem is not the regex itself but where the HTML is coming from. ’s original code uses HttpWebRequest, which performs its own HTTP fetch and does not share the WebBrowser control’s logged-in session or the DOM that the embedded browser builds (including any JavaScript/AJAX changes). was right to point toward using the WebBrowser.Document after navigation completes — that is the state that contains the page seen after manual login.

Use the WebBrowser DOM (preferred) or grab its InnerHtml after the final DocumentCompleted, then extract items. DOM access is more robust than regex when dealing with inputs and checkboxes. Example DOM approach (run this after the final DocumentCompleted):

' DOM approach: run after DocumentCompleted for the target page
For Each el As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
    If String.Equals(el.GetAttribute("type"), "checkbox", StringComparison.OrdinalIgnoreCase) Then
        CheckedListBox1.Items.Add(el.GetAttribute("value"))
    End If
Next

If a simple regex is still wanted, read the browser HTML and match against it (less robust for messy HTML or JS-modified content):

Dim html As String = WebBrowser1.Document.Body.InnerHtml
Dim pattern As String = "<input[^>]*type=[""']?checkbox[""']?[^>]*value=[""']([^""']+)[""']"
For Each m As Match In Regex.Matches(html, pattern, RegexOptions.IgnoreCase)
    CheckedListBox1.Items.Add(m.Groups(1).Value)
Next

Troubleshooting notes: DocumentCompleted fires for each frame — confirm e.Url equals WebBrowser1.Document.Url (or WebBrowser1.Url) before parsing. If content is injected by AJAX, wait for the specific element to exist (or use a small timer) because the page can be “loaded” while dynamic content is still populating. If a programmatic (non-browser) login is required, use HttpWebRequest with a CookieContainer and post the login form; copying WinINet cookies from WebBrowser to HttpWebRequest is non-trivial. Prefer DOM parsing or an HTML parser library for reliability over brittle regex.

Recommended Answers

All 3 Replies

If you just need to extract data from a webpage, see if this helps to extract the "Tags" from the "Tag Cloud" of this page.
New Project, 1 ListBox, 1 WebBrowser.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://www.daniweb.com/forums/thread335473.html")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        getItems(WebBrowser1.Document.Body.InnerHtml.Split(vbCrLf), ListBox1)
    End Sub

    Private Sub getItems(ByVal arLines() As String, ByVal selectedListBox As ListBox)
        selectedListBox.Items.Clear() '// Clear in case the webpage reloads.
        For i As Integer = 0 To arLines.Length - 1 '// Loop thru all Strings in the Array.
            If arLines(i).Contains("<DIV class=tagcloud>") Then '// Locate the String that .Contains...
                Dim arTemp() As String = arLines(i).Split(">") '// .Split the entire String into Arrays.
                For Each itm As String In arTemp '// Loop thru all Strings in the Array.
                    If itm.EndsWith("</A") Then '// check if String .EndsWith("</A")
                        itm = itm.Replace("</A", "") '// Remove the HTML Code from String.
                        selectedListBox.Items.Add(itm) '// add Tag Name to ListBox.
                    End If
                Next
                Exit For '// Exit the Loop.
            End If
        Next
    End Sub
End Class

Thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.