Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Extract multiple numbers from a string

Tags:

c#

regex

parsing

I'm trying to extract whole numbers of different length from a string with lots of formatting. The string in question could look like this:

string s = "Hallo (221122321 434334 more text3434 even mor,34343 343421.343sf 343";

The output I'm looking for is an array of:

{221122321,434334,3434,34343,343421,343,343}
like image 591
Ásgeir Gunnar Stefánsson Avatar asked Nov 04 '13 13:11

Ásgeir Gunnar Stefánsson


1 Answers

var result = new Regex(@"\d+").Matches(s)
                              .Cast<Match>()
                              .Select(m => Int32.Parse(m.Value))
                              .ToArray();
like image 128
decPL Avatar answered Sep 23 '22 19:09

decPL