2016-03-05 48 views
0

我想提出一個代理刮板程序的某些字符串,我需要找到代理服務器陣列中的C#中找到一個數組

這裏是什麼,我想擺脫這一行的例子:

document.write('77.237.138.51')

我想刪除document.write('" and "')所以它只顯示了代理

這裏是我當前的代碼:

client.DownloadFile("http://www.gatherproxy.com/sockslist", "source.txt"); 
string [] lines = File.ReadAllLines("source.txt"); 
string start = "document.write('"; 
string end = "')"; 

現在我將如何使它所以在那裏我可以刪除開始和結束並返回中間元素(代理)

在回答Domysee

using (WebClient client = new WebClient()) 
      client.DownloadFile("http://www.gatherproxy.com/sockslist", "source.txt"); 
      string[] lines = File.ReadAllLines("source.txt"); 
     for (int i = 0; i < 1000; i++) 
     { 
      string[] ipAddresses = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray(); 
      i++; 
      string[] port = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray(); 
      Console.WriteLine(ipAddresses + ":" + port); 
     } 
      Console.ReadLine(); 
+0

所以的Source.txt所包含的行格式 「文件撰寫('11 .111.111.11' )」,你需要 「11.111.111.11」 的呢? – Domysee

+0

是的,這是正確的。 – Zezima

+0

您的源文件包含其他具有IP地址的行? – Enigmativity

回答

1

您可以利用Regex用於這一目的。

string[] ipAddresses = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray(); 

正則表達式將提取與ip地址對應的位。

ipAddresses是一個字符串數組。如果將它與另一個字符串連接起來(如Console.WriteLine(ipAddresses + ":" + port);中所做的那樣,將調用其ToString方法,即「System.String []」。

要輸出IP地址,您必須遍歷數組。

string[] lines = File.ReadAllLines("source.txt"); 
string[] ipAddresses = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray(); 
for(int i = 0; i < ipAddresses.Length; i++){ 
    Console.WriteLine(ipAddresses[i]); 
} 
+0

我只是不會重複使用'lines'。變量很便宜。 –

+0

@HenkHolterman是的,你是對的,改變它 – Domysee

+0

System.String []返回而不是IP地址。我錯過了什麼嗎? – Zezima

0

你可以使用LINQ:

string[] lines = File.ReadAllLines("source.txt"); 

string[] ipAddresses = lines.Select(line => String.Join("", line.SkipWhile(c => c != '\'') 
                   .Skip(1) 
                   .TakeWhile(c => c != '\''))) 
          .ToArray();