VB.NET - Getting String Between 2 Strings

gimme4free

Elite Member
Joined
Oct 22, 2008
Messages
1,935
Reaction score
1,989
I have 2 functions, 1 which I converted from PHP - VB that uses Regex (Requires System.Text.RegularExpressions). The other which was shared by a member on this site.

The problem that I am facing is that neither of the functions allow you to get the string between 2 escaped characters ("").

Function 1 (Regex):
Code:
Public Function GetStringBetween(ByVal Haystack As String, ByVal StartSearch As String, ByVal EndSearch As String) As String
        Dim rx As New Regex(StartSearch & "(.+?)" & EndSearch)
        Dim m As Match = rx.Match(Haystack)
        If m.Success Then
            Return m.Groups(1).ToString()
        Else
            Return False
        End If
    End Function


Function 2:
Code:
Public Function GetBetween(ByVal haystack As String, ByVal needle As String, ByVal needle_two As String) As String
        Dim istart As Integer = InStr(haystack, needle)
        If istart > 0 Then
            Dim istop As Integer = InStr(istart, haystack, needle_two)
            If istop > 0 Then
                Try
                    Dim value As String = haystack.Substring(istart + Len(needle) - 1, istop - istart - Len(needle))
                    Return value
                Catch ex As Exception
                    Return ""
                End Try
            End If
        End If
        Return ""
    End Function

Example string:
Code:
Dim Haystack As String = "<input type=""hidden"" name=""testsubmit"" id=""testsubmit"" value=""findme""  />"
Dim FindIt As String = GetBetween(Haystack, "id=""testsubmit"" value=""", """ ")
MessageBox.Show(FindIt)


Neither of the above functions will find the 'findme' that it should be able to pull out whereas both functions will work on a simple string such as, "Hello Find Me World".

Also, I don't suppose that anyone here has a function that they wouldn't mind sharing that will find ALL occurences of a string between 2 strings and return in array format?
 
why not do a replace (chr(34)+chr(34),chr(34)) to change all those "" into " and then your function should work?
 
why not do a replace (chr(34)+chr(34),chr(34)) to change all those "" into " and then your function should work?
Managed to get some help over at SF on this one.

Changing line:
Dim istop As Integer = InStr(istart, haystack, needle_two)

To:
Dim istop As Integer = InStr(istart + Len(needle), haystack, needle_two)

Fixed the issue :)
 
I use my function all the time to parse HTML with it (mine is the Function 2 example you have above.

This is how to parse out html value between quotes

We'll assume this is your strHTML variable
Code:
<input type="hidden" name="testsubmit" id="testsubmit" value="findme"  />
and we wanted to get the value testsubmit from the name field
all you have to do is this

Code:
Dim needle as string = "name="""
Dim needle_two as string = """ id="""
dim strName as string = GetBetween(strHTML, needle, needle_two)

what is happening here is in order to pass along the escape character " in this case you must encase it in quotes at each point it appears in your needle string. so name=" turns into "name=""" which effectively passes the start quote along to your parsing function GetBetween. Same thing goes for the end. I like to be sure with my ending needle so I included part of the next attribute in the closing needle but if you wanted to trim it down you could have made needle_two this """" which is (1) "

I hope that helps out man.
 
Last edited:
Back
Top