2013-05-05 97 views
0

我通过串口通信在字符串中接收我的数据。那部分工作正常。的数据是在以下格式:使用正则表达式提取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; 
} 

但是不起作用。我会感谢您的任何帮助和建议。提前致谢。

+1

,我相信你是推超出了休闲使用正则表达式。你有没有考虑过自己的解析器?语法很简单,简单的解析就是每个开发人员工具箱中的技能。 – 2013-05-05 20:27:25

+0

你可以给一个示例代码来做这个操作吗?提前致谢。 – user2352673 2013-05-06 02:49:23

+0

向费马表示道歉:我已经找到一种非常优雅的方式向你展示这件作品;不幸的是,这个评论的边界太小而不能包含它。 – 2013-05-06 03:04:35

回答

2

您可以通过在{}之内传递2个值来定义变量重复,例如, \d{1,3}将匹配1-3位数字。

但总体而言,我可能会使用这个少正则表达式,给出的格式是相当不错的解析:

所有的
  • 首先我经历了所有行作为一个字符串数组就不断循环。
  • 如果一行为空,则忽略它,如果连续两行为空,则会创建一个新的数据集。
  • 如果该行包含一个:,则将该行分为两部分:左侧部分变为关键字,右侧部分变为值。
  • 如果该行不包含:它会被忽略或引发错误标志(或抛出的异常或其他)。
  • 然后,您可以使用简单的字符串匹配(比正则表达式更快)来确定“密钥”的含义。
  • 另一个正则表达式(或只是像Double.Parse())然后从右边提取值。请记住,这些转换函数将简单地跳过无效字符并删除跟随它们的任何内容。
  • 然后可以使用一个简单的正则表达式来解析更复杂的条目(如坐标或时间戳;或单位重要的条目)。

简化的代码(不一定是100%可编译/正确):

String[] lines = fileContents.Split({'\n'}); // split the content into separate lines 

bool wasEmpty = false; 
foreach (String line in lines) { 
    line = line.Trim(); // remove leading/trailing whitespaces 
    if (line.Length == 0) { // line is empty 
     if (wasEmpty) { // last line was empty, too 
      // init a new dataset 
     } 
     else 
      wasEmpty = true; 
     continue; // skip to next entry 
    } 
    wasEmpty = false; 
    String content = line.split({':'}); // split the line into a key/value pair 
    if (content.Length != 2) // not exactly two entries 
     continue; // skip 
    // content[0] now has the "key" (like "Lat/Long") 
    // content[1] now has the "value" (like "18.38891, -66.12175") 
    // both can be evaluated using regular expressions 
} 
+0

你能举一个代码示例吗? – user2352673 2013-05-06 01:56:26

+0

增加了一些示例代码。 – Mario 2013-05-06 09:53:42

+0

昨晚我在这工作,但没有100%的工作。当i = 0进入第一个时,如果做得很好的话。然后它应该去其他人,如果比较什么也不做,然后增加i并且再次评估,这次它假设进入第一个其他人,如果但没有进入。任何建议? – user2352673 2013-05-06 14:16:34

2

在您的正则表达式中,第二部分的名称是degress。在代码中,您使用的是degree

在您的正则表达式中,第二部分的名称是latlong。在代码中,您正在使用Lat/Long

因此,不,因为你将无法获得这两个组。

+0

当我从西班牙文翻译成英文时,这是一个错字。在代码中是正确的。 – user2352673 2013-05-06 02:57:23

相关问题