我有一个字符串string rx;
,该字符串具有以下类型的数据:“v=123.111i=10.123r=1234\r\n”
所以,我想要的是:-有一个 3 个浮点(或其他一些十进制变量)变量,“v”i”和“r”。-扫描字符串......使用像“v=%.3fi=”这样的格式字符串%.3fr=%.3f\r\n" 其中 %.3f 是值(3 个十进制数字)
var result = Regex.Matches(rx, @"\d+([,.]\d{1,3})?");
使用Regex
和Double.Parse
:
var inputString = @"v=123.111i=10.123r=1234\r\n";
foreach (Match match in Regex.Matches(inputString, @"\d+[.]?\d{3}"))
{
double result = Double.Parse(match.Value);
}
解释:
\d+ digits (0-9)
(1 or more times, matching the most amount possible)
[.]? character of: '.'
(optional, matching the most amount possible)
\d{3} digits (0-9)
(3 times)
我用过Double.Parse
。它将string
数字的表示形式转换为其等效的双精度浮点数。