-1

I have string input from a file that looks like this, and I have created a regex that successfully extracts just this:

 addresses { 1.1.1.1;
  2.2.2.2;
  3.3.3.3;
 }

And there can be arbitrary whitespace on any line. I'd like to get a List<string> of just the address values:

s[0] = "1.1.1.1"
s[1] = "2.2.2.2"
etc...

Can anyone help me with the relevant C#? I'm at the limits of my regex skills.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Rocky
  • 494
  • 6
  • 18

2 Answers2

1

You need just get content of curly braces (either with regex or simply with IndexOf) and split it by ;

var input = @"addresses { 1.1.1.1; 2.2.2.2; 3.3.3.3; }";
var regex = new Regex(@"[^{]+{([^}]+)}", RegexOptions.Multiline);
var addresses = regex.Match(input).Groups[1].Value
    .Split(';')
    .Select(s => s.Trim())
    .Where(s => !String.IsNullOrWhiteSpace(s))
    .ToList();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    Exactly the kind of elegant solution I was hoping to create. I had so much trouble with the proper syntax. Thanks! – Rocky Jan 06 '17 at 03:01
0

There could be n Number of ways you can extract the values from the string. This could be one of them.

string input = @"addresses { 1.1.1.1; 2.2.2.2; 3.3.3.3; }";
string formattedInput = Regex.Replace(input, @"[A-Za-z*{}]", "");
List<string> InpList = formattedInput.Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
Mohit S
  • 13,723
  • 6
  • 34
  • 69