How can i parse ip addresses from a file? I tried to put all the data in the array, then from this array to parse the ip address, but nothing worked. If not difficult, provide an example.

PS File with ip addresses looks like this:

Text 127.0.0.1 and further text.

PSS Please write an example in detail.

  • An example of the file would be given in its entirety (well, or described in more detail how it looks). And then you want a ready-made solution with unknown input data. - Monk
  • Input: text text 127.0.0.1 48691 text. Output: 127.0.0.1. And these lines are about 20-30k. - Bagger
  • Is there an IPv6? Is there a subnet? What exactly did you fail? - andreymal

1 answer 1

http://ideone.com/eW5yrp

using System; using System.Collections.Generic; using System.Text.RegularExpressions; public class Test { public static void Main() { string cur; var ips = new List<string>(); while ((cur = Console.ReadLine()) != null) foreach (Match m in Regex.Matches(cur, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) ips.Add(m.Value); foreach (var ip in ips) Console.WriteLine(ip); } }