Results 1 to 7 of 7

Thread: Extracting text from <label> tag

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Extracting text from <label> tag

    Hi fellow vb-ers,

    I'm trying to extract the text between:
    <label for=field_1> and </label>

    I have captured the HTML into a string using the webbrowser control, and am using the following function to try and get the text within the tag:
    Code:
    Public Function midReturn(ByVal first As String, ByVal last As String, ByVal total As String) As String
            If last.Length < 1 Then
                midReturn = total.Substring(total.IndexOf(first))
            End If
            If first.Length < 1 Then
                midReturn = total.Substring(0, (total.IndexOf(last)))
            End If
            Try
                midReturn = ((total.Substring(total.IndexOf(first), (total.IndexOf(last) - total.IndexOf(first)))).Replace(first, "")).Replace(last, "")
            Catch ArgumentOutOfRangeException As Exception
            End Try
        End Function
    However, this code will not work, if I use <label for=field_1> as the first string and </label> as the second... I'm a n00b to visual basic, can anybody help me out please?

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: Extracting text from <label> tag

    I think the problem is quite likely that </label> appears many times within the code.. before and after the snippet I'm trying to get.?

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Extracting text from <label> tag

    Try this method
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Extracting text from <label> tag

    Code:
    Imports System.Text.RegularExpressions
    Code:
        Private Function GetBetween(ByVal start As String, ByVal final As String, ByVal whatFrom As String) As String
            Return Regex.Match(whatFrom, "(?<=" & start & ").+(?=" & final & ")").Value
        End Function
    Last edited by J-Deezy; Oct 29th, 2010 at 06:39 AM.
    If I helped you out, please take the time to rate me

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: Extracting text from <label> tag

    Hi thankyou for your reply,

    When I use the code you provided, I get the following error:
    Code:
    Array bounds cannot appear in type specifiers.
    This is referring to the 'whatFrom'..?

  6. #6
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Extracting text from <label> tag

    See my edit, I wrote it out via the forum, not in an IDE so I illegally named a variable without realising (originally used "end" etc) the modified version should work.
    If I helped you out, please take the time to rate me

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Extracting text from <label> tag

    Hey,

    I am going to go out on a limb, and say that you are parsing an HTML string. If you are, you might want to think about using this HTML Agility Pack:

    http://htmlagilitypack.codeplex.com/

    It does a lot of the heavy lifting of parsing the HTML for you.

    Hope that helps!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width