I would like to get all 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);
}
}
You want to do Matches
. You will only have one group with that pattern.
如果您不拘泥于正则表达式,则更简单的方法是:
var digits = someString.Where(c => char.IsDigit(c)).ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.