|
-
Oct 29th, 2010, 05:54 AM
#1
Thread Starter
New Member
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?
-
Oct 29th, 2010, 06:13 AM
#2
Thread Starter
New Member
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.?
-
Oct 29th, 2010, 06:27 AM
#3
Re: Extracting text from <label> tag
Please mark you thread resolved using the Thread Tools as shown
-
Oct 29th, 2010, 06:31 AM
#4
Fanatic Member
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 
-
Oct 29th, 2010, 07:38 AM
#5
Thread Starter
New Member
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'..?
-
Oct 29th, 2010, 01:34 PM
#6
Fanatic Member
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 
-
Oct 29th, 2010, 01:55 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|