s='''Utilization (% of port capacity) 7.30 17.98
Utilization (% of port capacity) 5.60 20.98
Utilization (% of port capacity) 5.80 98.65
'''
如何利用正则匹配到这两个数,然后相加呢?
为什么非要正则呢..
string content = @"Utilization (% of port capacity) 7.30 17.98 Utilization(% of port capacity) 5.60 20.98 Utilization(% of port capacity) 5.80 98.65"; Regex reg = new Regex("\\s+(\\d{1,2}\\.\\d{2})\\s+(\\d{2}\\.\\d{2})"); if (reg.IsMatch(content)) { MatchCollection matches = Regex.Matches(content, "\\s+(\\d{1,2}\\.\\d{2})\\s+(\\d{2}\\.\\d{2})"); foreach (Match item in matches) { string A = item.Groups[1].ToString(); string B = item.Groups[2].ToString(); double C = Convert.ToDouble(A) + Convert.ToDouble(B); MessageBox.Show(C.ToString()); } } else { MessageBox.Show("未匹配到~"); }