检查字符串是否包含VB.Net中的IPv4地址

时间:2016-01-16 15:59:57

标签: vb.net string

如何判断字符串是否包含 IPv4 地址。 我想在此字符串中获取所有 IP地址

例:

以下字符串包含4个IPv4地址:

2016-01-16 00:00 - [99.245.14.88] downloaded ...
2016-01-16 00:00 - [120.103.139.95] downloaded ...
2016-01-15 23:59 - [166.118.4.233] downloaded ...
2016-01-15 23:59 - [55.234.129.191] downloaded ...

1 个答案:

答案 0 :(得分:0)

以下是我刚刚为您提供的一些代码..

    Dim Regex As New System.Text.RegularExpressions.Regex("\[(([\d]{1,3}\.?){4})\]", System.Text.RegularExpressions.RegexOptions.Multiline)
    Dim matches As System.Text.RegularExpressions.MatchCollection = Regex.Matches(TextBox1.Text) 'Replace textbox with your string source

    Dim tmp As String = ""
    For Each match As System.Text.RegularExpressions.Match In matches
        'Do something with each IP : match.Groups(1).Value
        tmp &= match.Groups(1).Value & vbNewLine
    Next

    MsgBox(tmp)

它使用正则表达式,如果您要使用大量文本,您应该阅读它们的工作原理。如果这是你正在寻找的,不要忘记标记为答案和upvote。