首页 新闻 会员 周边

正则的问题,求大神

0
[待解决问题]

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

'''

如何利用正则匹配到这两个数,然后相加呢?

Jummy恒的主页 Jummy恒 | 初学一级 | 园豆:176
提问于:2017-08-14 15:57
< >
分享
所有回答(2)
0

为什么非要正则呢..

吴瑞祥 | 园豆:29449 (高人七级) | 2017-08-14 19:56
0
 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("未匹配到~");
            }
| 园豆:332 (菜鸟二级) | 2017-08-15 14:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册