日期:2014-05-17  浏览次数:20812 次

怎样从这里提取出来所有的数字???

Left Channel:
Frequency = 1.020 kHz
Level = 11.93 dBu
THD+N = 18.065 %

Right Channel:
Frequency = 1.020 kHz
Level = 11.94 dBu
THD+N = 18.049 %

Level Difference (L - R): -0.006 dB

Phase Difference (L - R): -0.01 deg



就是从上面这一段字符串中提取出来所有的数字,包括
1.020
11.93
18.065
1.020
11.94
18.049
-0.006
-0.01



------解决方案--------------------
正则表达式 (-?\d+)(\.\d+)?
------解决方案--------------------
C# code

   string tempStr = @"Left Channel:
                                Frequency = 1.020 kHz
                                Level = 11.93 dBu
                                THD+N = 18.065 %

                                Right Channel:
                                Frequency = 1.020 kHz
                                Level = 11.94 dBu
                                THD+N = 18.049 %

                                Level Difference (L - R): -0.006 
                                dB Phase Difference (L - R): -0.01 deg";
            string pattern = @"-?\d+\.?\d*";
            MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.Multiline|RegexOptions.IgnoreCase);
            for (int i = 0; i < mc.Count; i++)
            {
                string value = mc[i].Value;//循环得到数字
               
         
            }