简体   繁体   中英

c# find certain strings of an array

I am making a proxy scraper program and i need to find the proxies in an array

Here is an example of what i want to get out of this line:

document.write('77.237.138.51')

I want to remove document.write('" and "') so it only shows the proxy

Here is my current code:

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

Now how would I make it so where i can delete start and end and return the middle element (the proxy)

In reply to 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();

You can utilize Regex for this purpose.

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

The regex will extract the bit that corresponds to the ip address.

ipAddresses is an array of strings. If you concatenate it with another string (as you're doing in Console.WriteLine(ipAddresses + ":" + port); , its ToString method will be called, which is "System.String[]".

To output the ip addresses you have to iterate over the array.

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]);
}

You can use LINQ:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM