Question Extract Lottery Numbers from String

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I am attempting to code a Lottery Number Checker program...I need to extract each of the 6 winning numbers from a string and put each number into its own label or textbox.I used regex to get this far...I download the page source and use regex to get to the part of the source that has the numbers.

here is the string I need to extract the numbers from...keep in mind this string will change every drawing.

ball" title="2">2</span><span class="wnDash">-</span><span class="ball" title="5">5</span><span class="wnDash">-</span><span class="ball" title="33">33</span><span class="wnDash">-</span><span class="ball" title="45">45</span><span class="wnDash">-</span><span class="ball" title="48">48</span><span class="wnDash">-</span><span class="ball" title="50">50</span><span class="wnDash">-</span><span class="multiplier" title="x3">x3

any help with extracting the 6 numbers?

thank You
InkedGFX
 
While I am not yet familiar with regex, the way I would do this for a string of this small size and simplicity is to look for the unique similarities and see if it is logically possible to extract the data I need based on the addresses of those similarities.

In your case, you will notice that before each number comes the sub-string " title="

So what I have done is split the HTML string in a way that allows me to make use of this unique similarity. What divides the number I need from the unique similarity and the rest of the string is the quotation mark "

By splitting the HTML using this quotation mark, I have now created an easy way to attempt to extract the number I need from the splitArray based on the value of the entry preceding it.

Dim htmlString As String = "ball"" title=""2"">2</span><span class=""wnDash"">-</span><span class=""ball"" title=""5"">5</span>" _
                                 & "<span class=""wnDash"">-</span><span class=""ball"" title=""33"">33</span><span class=""wnDash"">-</span>" _
                                 & "<span class=""ball"" title=""45"">45</span><span class=""wnDash"">-</span><span class=""ball"" title=""48"">48" _
                                 & "</span><span class=""wnDash"">-</span><span class=""ball"" title=""50"">50</span><span class=""wnDash"">-</span>" _
                                 & "<span class=""multiplier"" title=""x3"">x3"


      Dim stringSplit() As String = htmlString.Split("""")
      Dim index, numbers(5) As Integer


      For i = 0 To stringSplit.Length - 1

         If stringSplit(i).Contains("title=") Then

            If Integer.TryParse(stringSplit(i + 1), numbers(index)) Then

               index += 1

               If index = 6 Then Exit For

            End If

         End If

      Next



While I'm sure there are other ways to achieve what you are looking for that can be used for different formats of HTML strings, this way will work specifically on the example string you provided.
 
While I'm sure there are other ways to achieve what you are looking for that can be used for different formats of HTML strings, this way will work specifically on the example string you provided.[/QUOTE]

I appreciate the help 22Degrees, but this doesnt work for what I am trying to do.....well actually it gets "System.Int32[]" 6 times, how do I convert this to an integer?

Inkedgfx
 
If the format of the string is the same as the format you gave in the example, then after running the code, the numbers() array will have your 6 extracted numbers.

in case your interested here is the code...I modified your code a bit to get what I needed...THANK YOU! , I wouldnt have been able to do it without your help.

Dim SourceNumbers As String = GetWinningNumbersFromWebSite()
Dim src As String
src = SourceNumbers
Dim m As Match = Regex.Match(src, "\btitle\b.*\d+")

Dim stringSplit() As String = m.Value.Split("""")
Dim Index, numbers(5) As Integer
For i As Integer = 0 To stringSplit.Length - 1
If stringSplit(i).Contains("title=") Then
If Integer.TryParse(stringSplit(i + 1), numbers(Index)) Then
Index += 1

TextBox2.Text = numbers(0)
TextBox3.Text = numbers(1)
TextBox4.Text = numbers(2)
TextBox5.Text = numbers(3)
TextBox6.Text = numbers(4)
TextBox7.Text = numbers(5)
If Index = 6 Then Exit For
End If
End If
Next

InkedGFX
 
Back
Top