0

I want to remove all string except string startwith EVOPB-
how can I make it happen ?

enter image description here

Private Sub StringResult()
    Try
        Dim web As New HtmlDocument()
        web.Load(WebBrowser1.DocumentStream)
        '' Extracting All Links
        Dim redeem As HtmlNode = web.DocumentNode.SelectSingleNode("//div[@class='_58b7']")
        If (redeem.InnerText.Contains("")) Then
            Dim r As String = redeem.InnerText.ToString.Replace(vbNewLine, "")
            TextBox1.Text = r
        End If
    Catch
        Return
    End Try
End Sub
  • This looks like a good example where regular expressions come in handy. Besides the "starting characters" you'll need to define up to where you want the text to match. For instance "text starting with EVOPB, and all alphanumeric and dash characters that immediately follow it" (there are tons of tutorials about regular expressions, and even online testers you can use before writing the code). – C.Evenhuis Apr 02 '20 at 09:47
  • 1
    Though with a nod to [this iconic question](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) just incase anyone gets any ideas about taking it up a notch. It would be handy to see the source HTML actually, as these things might al be in the same kind of node – Caius Jard Apr 02 '20 at 09:50
  • If you have this content stored in a VB.net string already, you can just split the string using the `Space` character as delimeter. Then, from the resulting array, extract all entries that start with `EVOPB` and maybe concatenate them back to a string. This can be done with a simple LINQ statement. – preciousbetine Apr 02 '20 at 10:07

1 Answers1

1

Assuming what you are trying to match always starts with the same prefix and runs until the next space, something like this would work:

Public Shared Function ExtractStartsWith(ByVal Output As String, Optional StartsWith As String = "EVOPB") As List(Of String)

    Dim pos As Integer = 0
    Dim nextSpace As Integer
    Dim results As New List(Of String)
    Dim result As String
    Do While pos >= 0 AndAlso pos < Output.Length
        pos = Output.IndexOf(StartsWith, pos)
        If pos >= 0 Then
            nextSpace = Output.IndexOf(" ", pos)
            If nextSpace > 0 Then
                result = Output.Substring(pos, nextSpace - pos)
                pos = nextSpace + 1
            Else
                result = Output.Substring(pos)
                pos = Output.Length
            End If
            results.Add(result)
        End If
    Loop
    Return results
End Function
Jon Roberts
  • 2,262
  • 2
  • 13
  • 17