字符处理问题,帮忙看下
--------数据1-----------
- 10.0, g
ST,GS,- 10.0, g
ST,GS,- 10.0, g
ST,GS,- 10.0, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS, 107.2, g
ST,GS,- 10.0,
-------数据2-------------
, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS, 118.0, g
ST,GS,
我上面这两组读数有没有简单点的办法取出数字类型的107.2或者-10.0这样的数
我现在都是字符匹配出来“ 118.0”或者“,- 10.0”然后再转换为数字。
有没更简单的办法
------解决方案--------------------
C# code
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string pattern = @"(?i)(?<=ST.*?)(-\s*?)?\d+(\.\d+)?(?=.*?k?g)";
List<string> result_list = Regex.Matches(tempStr, pattern).Cast<Match>().Select(a => a.Value).Distinct().ToList();