我想从一个字符串中提取多个数字。该字符串可能如下所示:
hello:123.11,good:456,bye:789.78
我想得到3个数字(包括整数和浮点数):C#的123.11、456、789.78。
更新:包括浮点数,但不全是整数。
如何?谢谢!
尝试使用带有regexMatches
的Regex
类的方法来获取所有出现的数字。
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var subjectString = "hello:123,good:456,bye:789";
var result = Regex.Matches(subjectString, @"[-+]?(\d*[.])?\d+");
foreach(var item in result)
{
Console.WriteLine(item);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句