none
How to extract IP and port Regex RRS feed

  • Question

  • How to extract IP and port in this format 

    167.71.167.227:8080

    from html and javascript

    <tr><td class="left" style="text-align: center;"><script type="text/javascript">document.write(Base64.decode("MTY3LjcxLjE2Ny4yMjc="))</script>167.71.167.227</td><td><span class="fport">8080</span></td><td><small>HTTP</small></td><td class="left"><div style="padding-"><img class="flag flag-us" alt="United States" src="/flags/blank.gif"> <a href="/en/proxylist/country/US/all/ping/all">United States</a></div></td><td class="small"><small>New York</small></td><td class="small"><small>New York</small></td><td class="small"><small>Anonymous</small></td><td> <small>6253 kB/s</small> <div class="progress"><div class="fill" style="width: 100%; background-color: rgb(165, 218, 116);"></div></div></td><td><small>100%</small><div class="progress"><div class="fill" style="width: 100%; background-color: rgb(165, 218, 116);"></div></div></td><td><div style="padding-"><small>48 ms</small> <div class="progress"><div class="fill" style="width: 99%; background-color: rgb(165, 218, 116);"></div></div></div></td><td><small>6 hours ago</small></td></tr>

    i use the code but not work

     Dim expression As New System.Text.RegularExpressions.Regex("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}")
    
                            Dim mtac As MatchCollection = expression.Matches(RichTextBox1.Text)
                            For Each itemcode As Match In mtac
                                ListBox1.Items.Add(itemcode)
                                Exit For
                            Next



    Wednesday, August 21, 2019 12:21 PM

Answers

  • Hi,

    Because your regular expression contains ":", but it is not included in your javascript, the result will not be as you want.  You need to add ":" manually.
    You can try my code, which implements using regular expressions to get the IP and Port you want:

    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim expression As New Regex("(?'IP'[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}).*>(?'port'[0-9]{1,5})</span>")
            Dim mtac As MatchCollection = expression.Matches(RichTextBox1.Text)
            For Each itemcode As Match In mtac
                ListBox1.Items.Add(itemcode.Groups("IP").ToString() & ":" & itemcode.Groups("port").ToString())
                Exit For
            Next
        End Sub
    End Class

    You can also look up relevant documents for help:
    In addition, if you want to get IP and Port from the URL of the web page, you can use it directly without modifying your code. The results are as follows:

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, August 22, 2019 8:13 AM

All replies

  • Check this:

    Dim pattern = "</script>(?'ip'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td><td><spanfport"">(?'port'\d+)</span>"
     
    For Each m As Match In Regex.Matches(example.Value, pattern, RegexOptions.IgnoreCase)
     
        Dim itemcode = m.Groups("ip").Value & ":" & m.Groups("port").Value
     
        ' TODO: add to Listbox
     
    Next
     

    Adjust the pattern for other cases.


    • Edited by Viorel_MVP Wednesday, August 21, 2019 5:52 PM
    Wednesday, August 21, 2019 5:51 PM
  • not work sir

         Dim pattern = "</script>(?'ip'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td><td><spanfport"">(?'port'\d+)</span>"
    
            For Each m As Match In Regex.Matches(RichTextBox1.Text, pattern, RegexOptions.IgnoreCase)
    
                Dim itemcode As String = m.Groups("ip").Value & ":" & m.Groups("port").Value
    
                ListBox1.Items.Add(itemcode)
            Next

    Wednesday, August 21, 2019 9:30 PM
  • Hi,

    Because your regular expression contains ":", but it is not included in your javascript, the result will not be as you want.  You need to add ":" manually.
    You can try my code, which implements using regular expressions to get the IP and Port you want:

    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim expression As New Regex("(?'IP'[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}).*>(?'port'[0-9]{1,5})</span>")
            Dim mtac As MatchCollection = expression.Matches(RichTextBox1.Text)
            For Each itemcode As Match In mtac
                ListBox1.Items.Add(itemcode.Groups("IP").ToString() & ":" & itemcode.Groups("port").ToString())
                Exit For
            Next
        End Sub
    End Class

    You can also look up relevant documents for help:
    In addition, if you want to get IP and Port from the URL of the web page, you can use it directly without modifying your code. The results are as follows:

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, August 22, 2019 8:13 AM