Results 1 to 9 of 9

Thread: [RESOLVED] Retrieve string from between two strings loop

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    61

    Resolved [RESOLVED] Retrieve string from between two strings loop

    Hi all.. been looking online for a solution for this but with no luck. A few posts suggested regular expression but I couldn't work out how to implement it as my solution.

    I'm just looking for the easiest code which would do that i need.

    Lets say i have the below string.. which I'm reading from a html file.

    Code:
        <div class="c-price-band-filter__price">
                            <h2>What is your budget?</h2>
                            <div class="row">
                                <div class="medium-6 columns">
                                    <label for="ddlMinimumPrice" id="SiteMasterBody_ContentPlaceHolder1_uscSeatSelection_uscStandAndAreaSelection_lblMinimumPrice">Minimum Price</label><select name="ctl00$ctl00$SiteMasterBody$ContentPlaceHolder1$uscSeatSelection$uscStandAndAreaSelection$ddlMinimumPrice" id="ddlMinimumPrice" onchange="document.getElementById('SiteMasterBody_ContentPlaceHolder1_uscSeatSelection_uscStandAndAreaSelection_hdfSelectedMinimumPrice').value=this.value; ReDrawStadium(false, true, false, false, false, false);">
    		<option value="24.00" selected="selected">£24.00</option>
    		<option value="25.00">£25.00</option>
    		<option value="26.00">£26.00</option>
    		<option value="27.00">£27.00</option>
    		<option value="29.00">£29.00</option>
    		<option value="30.00">£30.00</option>
    
    	</select>
                                </div>
    I want to read through all of it and get each value between <option value=" and the next quote

    or... the very least the text between <option value= and </option>

    I want to add the values to a list of string... i.e 24.00, 25.00,26.00,27.00,29.00,30.00

    Any help would be greatly appreciated.
    Last edited by dday9; Feb 6th, 2018 at 09:34 AM.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Retrieve string from between two strings loop

    It looks like you're trying to write a ticket bot. That makes life worse for everyone.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    61

    Re: Retrieve string from between two strings loop

    Quote Originally Posted by Sitten Spynne View Post
    It looks like you're trying to write a ticket bot. That makes life worse for everyone.
    How very perceptive of you..but I actually work for a ticketing company and I'm doing load testing and need to get the option values to pass different values into my load test.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Retrieve string from between two strings loop

    Well, if you're with the company that made the website, then you've got the ideal situation. Parsing stuff out of pages is a misery at the best of times. RegEx can do it, but I've never head anybody say it was fun. Therefore, have them make you an API, if there isn't one already, and you can just have your values handed to you. That's certainly what I'd do. After all, the site owner might be dismayed if you could make a ticket bot, even for load testing, in which case they'd be strongly inclined to undermine your work by changing the HTML to prevent whatever you did. An API solves all that.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    61

    Re: Retrieve string from between two strings loop

    Quote Originally Posted by Shaggy Hiker View Post
    Well, if you're with the company that made the website, then you've got the ideal situation. Parsing stuff out of pages is a misery at the best of times. RegEx can do it, but I've never head anybody say it was fun. Therefore, have them make you an API, if there isn't one already, and you can just have your values handed to you. That's certainly what I'd do. After all, the site owner might be dismayed if you could make a ticket bot, even for load testing, in which case they'd be strongly inclined to undermine your work by changing the HTML to prevent whatever you did. An API solves all that.
    I am a software developer for a ticketing company. We host and own the websites. It's not a case of getting an API done. We have api's for pricing but different areas of our stadium have different pricing options.

    I'm trying to create a generic function though for my load testing in general. It isn't just for selecting prices dynamically. It's for many different things where options exist and those options may change.

    I am just trying to simulate customers selecting different prices amongst other things. Yes this sort of thing probably could be used to create a bot.

    I'll rephrase my question to me more generic somewhere else if nobody can help.

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Retrieve string from between two strings loop

    If you want to scrape HTML, your best bet's an HTML parser. The NuGet package for HTML Agility Pack is generally the best. It has a lot of documentation and ought to get you pretty far. That's the core of your problem. I'm sorry for being vague, it's just something we get a ton of and there really are already a lot of good tutorials and explanations of how that API works.

    In general, every web problem is an API problem, you just have to frame it correctly.

    What happens when you go to your page? Let's say it's an ASP .NET application. You issue a GET request, maybe with some cookies or some other authentication mechanism. That's all sent as text to your server. It starts up the ASP .NET engine and says "hey do what it takes to get this page". The ASP .NET code needs the prices for that particular thing. It asks the database, then spits out the prices into HTML. The browser gets the HTML and renders it. When the user makes a selection, some kind of encoded string gets sent to your server and the whole process starts again for a different page.

    That's... more or less how an API works. You send a request to a URL, it sends you text back. You do something with that text and send more text to a different URL, things happen and you get text back again.

    The reason I lay that out is it seems like there are tools for load-testing APIs, but I don't see a lot about tools for load-testing pages. This article details a couple. Some tools, like Fiddler, can record your web traffic then "play it back", which is more or less what your tool would eventually do. You may want to look into reusing someone else's work before spending a lot of time writing something custom.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Retrieve string from between two strings loop

    Hi,

    I placed you HTML in a Textfile(C:\Htmlread.txt) for testing

    play around with this...
    Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Using Writer As New System.IO.StreamWriter("C:\Output.txt")
                For Each Match As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(My.Computer.FileSystem.ReadAllText("C:\Htmlread.txt"), "<option value=.*?\</option>")
                    'or:
                    'For Each Match As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(My.Computer.FileSystem.ReadAllText("C:\Htmlread.txt"), ";.*?\</")
    
                    Writer.WriteLine(Match.Value)
                Next
            End Using
        End Sub
    see the results in C:\Output.txt

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Retrieve string from between two strings loop

    Parsing HTML with regex.

    Bonus negative points for replying with "works for me".
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    61

    Re: Retrieve string from between two strings loop

    I've written a

    Code:
        Public function OptionSplit(OptionString$, optional ByVal StrBegin$ = "", Optional byval StrEnd$ ="") As List(Of String)
              
            If StrBegin.Length > 0 AndAlso StrEnd.Length > 0 Then
            Dim iStart As Integer
            Dim iEnd As Integer
            Dim sValue As String = ""
                 'Return the form value
            iStart = OptionString.IndexOf(StrBegin, 0)
            If iStart >= 0 Then
                iEnd = OptionString.IndexOf(StrEnd, iStart)
                If iEnd >= 0 Then
                    'Set the value
                    OptionString = OptionString.Substring(iStart + StrBegin.Length, iEnd - iStart - StrBegin.Length)
                End If
            End If
            End If
    
    
            Dim TempList As new List(Of String)
            Dim OptionList As new List(Of String)
            
            Dim re1 As String = "(<option value)"       'start text
            Dim re2 As String = ".*?"                   'everything in the middle
            Dim re3 As String = "(>)"                   'end text
            Dim reg As String = re1 + re2 + re3
    
            Dim regex As New Text.RegularExpressions.Regex(reg)
           
            For Each m As Text.RegularExpressions.Match In regex.Matches(OptionString, RegexOptions.IgnoreCase)
                TempList.Add(m.Value)
            Next
    
            Dim TempSBuilder As New StringBuilder 
            Dim word As string
            For each word In TempList
                TempSBuilder.Append(word)
            Next
    
            OptionString = TempSBuilder.ToString
    
            Dim StringSplit() as String = OptionString.Split(""""c)
                 
                   
            For Each word In StringSplit
                If  UCase(word).Contains("SELECTED") or UCase(word).Contains("<") or UCase(word).Contains(">") Then
                    'Don't Add
                    Else
                    OptionList.Add(word)
                End If
            Next
            Return OptionList
        End function

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