开发者

regular expression : find all digit in a string

I would like to get a开发者_如何学运维ll digits of a string like this :

"0" => Groups = "0"
"1 2-3" => Groups = "1", "2", "3"
"45i6" => Groups = "4", "5", "6"

I'm using this code :

var pattern = @"(\d)";
var m = System.Text.RegularExpressions.Regex.Match(value, pattern);
if(m.Success)
{
    foreach (var gp in m.Groups)
    {
        Console.WriteLine(gp);
    }
}

Can you help me to get the good pattern please ?

Many thanks

OK, the good code is :

Thanks Daniel

I'm using this code :

var pattern = @"(\d)";
var ms = System.Text.RegularExpressions.Regex.Matches(value, pattern);
if(ms.Count > 0)
{
    foreach (var m in ms)
    {
        Console.WriteLine(m);
    }
}


If you aren't stuck on regular expressions, a more straightforward method would be:

var digits = someString.Where(c => char.IsDigit(c)).ToArray();


You want to do Matches. You will only have one group with that pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜