Sunday, 10 February 2013

get numbers from string c# Regular Expressions

get numbers from string c# Regular Expressions/get numbers from string c#

Suppose String is Alphanumeric, if user wants to extract Numbers only, in that case you can use Regular expressions to match digit patterns.

for ex: String str = "1abc34bcd677bsgdb7878ddh63d4d";
output: 
1
34
677
787
63

static void GetAllNumbersFromString()
{
String str = "1abc34bcd677bsgdb7878ddh63d4d";

MatchCollection mc=Regex.Matches(str, @"\d+");
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(mc[i].Value);
}
}

Note: One way of getting Numbers from a String

Tags:get numbers from string c# Regular Expressions,
get numbers from string c#,Regex.Matches.MatchCollection,Integer Arrays C#,Regular Expression Pattern matching digits only.