如何从字符串中获取代理

时间:2015-06-27 00:35:06

标签: regex vb.net

我有字符串

76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United
States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United 
States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States

以及我如何从中获取代理 我的代码

Dim ip As String = "76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States"
    ip = Regex.Match(ip, "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\:\d{2,5}\b", RegexOptions.Singleline).ToString
    RichTextBox1.Text = ip

它只显示第一个代理76.125.85.66:16805,但我希望它显示所有

76.125.85.66:16805

69.207.212.76:49233

96.42.127.190:25480

2 个答案:

答案 0 :(得分:0)

使用正则表达式Matches()方法,删除起始字边界。

您可以按如下方式编写:

For Each m As Match In Regex.Matches(ip, "(?:\d{1,3}\.){3}\d{1,3}:\d+")
    Console.WriteLine(m.Value)
Next

Ideone Demo

答案 1 :(得分:-1)

使用此模式返回特定表达式的多结果

public ArrayList HRefs(string incomingHtml)
    {
      ArrayList arrayList = new ArrayList();
      string pattern = "href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))";
      for (Match match = Regex.Match(incomingHtml, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); match.Success; match = match.NextMatch())
      {
        string str = match.Groups[1].Value;
        arrayList.Add(str);
      }
      return arrayList;
    }