2

Language = Visual Basic. I have a project that use .Net framework 4

I have this code for Regex:

Private Shared RegPattern As New Regex("\<base+.+?href\s*\=\s*(""(?<HREF>[^""]*)""|'(?<HREF>[^']*)')(\s*\w*\s*\=\s*(""[^""]*""|'[^']*')|[^>])*(\/>|>\<\/base\>)", RegexOptions.IgnoreCase Or RegexOptions.Singleline)

I have this function to get links from a html page:

Private Sub GetAdress(ByVal HtmlPage As String)
            Base = ""
            Dim Matches As System.Text.RegularExpressions.MatchCollection = RegPattern.Matches(HtmlPage)

            For Each V_Found As System.Text.RegularExpressions.Match In Matches
                Base = V_Found.Groups("HREF").Value           
End Sub

The function works fine but in some cases enter in a infinite loop. The debugger says "Evaluation Time out" at the line:

Dim Matches As System.Text.RegularExpressions.MatchCollection = RegPattern.Matches(HtmlPage)

and the exe not continue or exit or catch exceptions. How can i handle this problem? How can i exit from GetAddress method? I know there is timeoutexception but in net 4 i can't use it.

MarioProject
  • 265
  • 2
  • 22

1 Answers1

0

If you are wanting to keep the code, but catch the exception so it does nothing, try a Try...Catch.

Try
    Dim Matches As System.Text.RegularExpressions.MatchCollection = RegPattern.Matches(HtmlPage)
    Base = ""

    For Each V_Found As System.Text.RegularExpressions.Match In Matches Base = V_Found.Groups("HREF").Value
Catch TimeOutException
End Try

Since it looks like you are just trying to parse links, you could try something like:

Dim htmlBrowser As WebBrowser = 'Browser with HtmlPage'
Dim linkCollection As HtmlElementCollection = htmlBrowser.Document.GetElementsByTagname("a") 'Or another tag name

For Each elems As HtmlElement In linkCollection
    Base = ""
    Dim Matches As System.Text.RegularExpressions.MatchCollection = RegPattern.Matches(HtmlPage)

    For Each V_Found As System.Text.RegularExpressions.Match In Matches Base = V_Found.Groups("HREF").Value
        'Code to run'
    Next
Next
Tytus Strube
  • 27
  • 1
  • 7