我通过串口通信在字符串中接收我的数据。那部分工作正常。的数据是在以下格式:使用正则表达式提取c中字符串中的特定值#
Distance run: 36in.
Direction in degrees: 275
Total of person founds:11
New Person found in:
Lat/Long: 18.38891, -66.12174
Date: 5/4/2013 Time: 19:13:35.0
Total of person founds:12
New Person found in:
Lat/Long: 18.38891, -66.12175
Date: 5/4/2013 Time: 19:13:37.0
Distance run: 15in.
Direction in degrees: 215
Total of person founds:13
New Person found in:
Lat/Long: 18.38891, -66.12174
Date: 5/4/2013 Time: 19:13:39.0
Distance run: 30in.
Direction in degrees: 180
但是,这可以在人创立的每个博客(其中包括它在纬度/经度,日期和时间位置)之间会有一些变化原因可以有另一个距离跑和度数方向。
我试过正则表达式,但不知道如何使用它。我甚至有一个只提取数字的正则表达式。
var xnumbers = Regex.Split(strFileName, @"[^0-9\.\-]+").Where(c => c != "." && c.Trim() != "");
我想要的是提取特定的值,比如说跑步距离:第一个值是36in并存储它等等。然后以度为单位获取方向的值,并将其存储在另一个变量中,最后获取经纬度并将其存储在其他变量中。我需要这个值来创建一个List来稍后使用这些数据并绘制它。我已经有绘图部分。
我尝试这样做:
我知道这个图案仅需考虑该距离仅2个电话号码,但该值可以是1或3个数字(例如:距离执行命令1英寸或距离运行:在219)
string pattern = @"^Distance Run:(?<distance>.{2}),Direction in degrees:,(?<degress>. {3}),Lat/Long:(\+|\-)(?<latlong>.{18})$";
string distance = string.Empty;
string degrees = string.Empty;
string latlong = string.Empty;
Regex regex = new Regex(pattern);
if (regex.IsMatch(strFileName)) // strFileName is the string with the data
{
Match match = regex.Match(strFileName);
foreach (Capture capture in match.Groups["distance"].Captures)
distance = capture.Value;
foreach (Capture capture in match.Groups["degree"].Captures)
degrees = capture.Value;
foreach (Capture capture in match.Groups["Lat/Long"].Captures)
latlong = capture.Value;
}
但是不起作用。我会感谢您的任何帮助和建议。提前致谢。
,我相信你是推超出了休闲使用正则表达式。你有没有考虑过自己的解析器?语法很简单,简单的解析就是每个开发人员工具箱中的技能。 – 2013-05-05 20:27:25
你可以给一个示例代码来做这个操作吗?提前致谢。 – user2352673 2013-05-06 02:49:23
向费马表示道歉:我已经找到一种非常优雅的方式向你展示这件作品;不幸的是,这个评论的边界太小而不能包含它。 – 2013-05-06 03:04:35