.NET Find string in between other strings

Hello, I have a program that downloads the source of a webpage. I need to get a string in between other strings. I want to specify the beginning and end, and then get the in between.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do a quick Google search on vb regular expressions and it will be easy.
lean0nmeAuthor Commented:
I did and have gotten something which works, but for some reason breaks if there is a lot of text. Here is the code:

Function Mybetween(ByVal src As String, ByVal first As String, ByVal last As String) As String
        MsgBox("src is " & src)
        MsgBox("Start string is " & first)
        MsgBox("End string is " & last)
            src = src.Replace(first, "|")
            src = src.Replace(last, "|")
            Return src.Split(New Char() {"|"c})(1)

        Catch ex As Exception

        End Try

    End Function

    Function Between(ByVal src As String, ByVal first As String, ByVal last As String) As String
        Dim startloc As Integer = InStr(src, first, 0)
        Dim endloc As Integer = InStr(Strings.Mid(src, (startloc + 1)), last, 0)
        Return Mid(src, (startloc + 1), (endloc - 1))
    End Function
    Function between(ByVal src As String, ByVal c_start As Char, ByVal c_end As Char) As String
        Dim ret = String.Empty
        Dim idxStart = src.IndexOf(c_start)
        If idxStart <> -1 Then
            idxStart += idxStart
            Dim idxEnd = src.IndexOf(c_end, idxStart)
            If idxEnd <> -1 Then
                ret = src.Substring(idxStart, idxEnd - idxStart)
            End If
        End If
        Return ret
    End Function

Open in new window

You have multiple options.

1) Regular Expressions
2) HTML Parsing
3) String manipulation.

For 3, you can use indexof, substring etc to find the location of string. Indexof takes a parameter to specify the start of search. So index of first string can be used to find index of search string which occurs after the first string.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Your code has three different attempts in it. The first one looks the simplest and should work.

When you call MyBetween, does it give an error? It looks like it should work.
lean0nmeAuthor Commented:
Sorry, the code I'm using is MyBetween. It works, but stops working with a lot of text for some reason.
Add this line
 MsgBox ex.Message
Right after the line that says Catch ex As Exception

Then you can probably see why it isn't working.
Otherwise, what is it doing when it "stops working"?
lean0nmeAuthor Commented:
It doesn't give an error, just returns a null result.
Output the src and see if it's finding the other strings.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lean0nmeAuthor Commented:
I used the Between method and it works. So weird because it didn't work before which is why I was using mybetween.

Thank you.
lean0nmeAuthor Commented:
Thanks for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.