C# 使用正则表达式在c中提取字符串中的特定值#
我通过串口通信接收字符串中的数据。那部分很好用。数据格式如下:C# 使用正则表达式在c中提取字符串中的特定值#,c#,regex,string,data-extraction,C#,Regex,String,Data Extraction,我通过串口通信接收字符串中的数据。那部分很好用。数据格式如下: 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.
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
但这可能会有一点不同,因为在每个人的博客中(包括它在lat/long、date和time中的位置),可能会有另一个以度为单位的长跑和方向
我试过正则表达式,但不知道如何使用它。我甚至有一个只提取数字的正则表达式
var xnumbers = Regex.Split(strFileName, @"[^0-9\.\-]+").Where(c => c != "." && c.Trim() != "");
我想要的是提取distance run的具体值:第一个值是36英寸,然后存储它,以此类推。然后以度为单位获得方向值,并将其存储在另一个变量中,最后获得lat&long并将其存储在另一个变量中。我需要这些值来创建一个列表,以便以后使用该数据并绘制它。我已经有绘图部分了
我试过这个:
我知道这种模式只考虑到距离是2个数字,但该值可以是1或3个数字(例如:长跑:1in或长跑:219in)
string pattern=@“^Distance Run:(?。{2}),以度为单位的方向:,(?。{3}),Lat/Long:(\+\124; \-)(?。{18})$”;
字符串距离=string.Empty;
字符串度=string.Empty;
string latlong=string.Empty;
正则表达式正则表达式=新正则表达式(模式);
if(regex.IsMatch(strFileName))//strFileName是包含数据的字符串
{
Match=regex.Match(strFileName);
foreach(在match.Groups[“distance”]中捕获捕获。捕获)
距离=捕获值;
foreach(在match.Groups[“degree”]中捕获捕获。捕获)
度=捕获值;
foreach(在match.Groups[“Lat/Long”].捕获中捕获)
latlong=capture.Value;
}
但它不起作用。谢谢你的帮助和建议。提前感谢。在正则表达式中,第二部分的名称是
degress
。在代码中,您使用的是degree
在正则表达式中,第二部分的名称是
latlong
。在代码中,您使用的是Lat/Long
因此,不,按原样,您将无法获得这两个组。在正则表达式中,第二部分的名称是
degress
。在代码中,您使用的是degree
在正则表达式中,第二部分的名称是
latlong
。在代码中,您使用的是Lat/Long
所以不,按原样,您将无法获得这两个组。您可以通过在
{}
中传递2个值来定义变量重复,例如,\d{1,3}
将匹配1-3个数字
但总的来说,我可能会少用正则表达式,因为这种格式很容易解析:
- 首先,我将以字符串数组的形式遍历所有行
- 如果一行为空,则忽略它;如果两个连续行为空,则创建一个新的数据集
- 如果该行包含一个
则将该行拆分为:左侧部分成为键,右侧部分为值:
- 如果该行不包含
,则会忽略该行,或引发错误标志(或引发异常或其他):
- 然后可以使用简单的字符串匹配(比正则表达式快)来确定“键”的含义
- 另一个正则表达式(或者类似于
)从右侧提取值。请记住,这些转换函数只会跳过无效字符并删除任何尾随它们的字符Double.Parse()
- 然后可以使用简单的正则表达式解析更复杂的条目(如坐标或时间戳;或者单位很重要的条目)
简化代码(不一定100%可编译/正确):
您可以通过在
{}
中传递2个值来定义变量重复,例如\d{1,3}
将匹配1-3个数字
但总的来说,我可能会少用正则表达式,因为这种格式很容易解析:
- 首先,我将以字符串数组的形式遍历所有行
- 如果一行为空,则忽略它;如果两个连续行为空,则创建一个新的数据集
- 如果该行包含一个
则将该行拆分为:左侧部分成为键,右侧部分为值:
- 如果该行不包含
,则会忽略该行,或引发错误标志(或引发异常或其他):
- 然后可以使用简单的字符串匹配(比正则表达式快)来确定“键”的含义
- 另一个正则表达式(或者类似于
)从右侧提取值。请记住,这些转换函数只会跳过无效字符并删除任何尾随它们的字符Double.Parse()
- 然后可以使用简单的正则表达式解析更复杂的条目(如坐标或时间戳;或者单位很重要的条目)
简化代码(不一定100%可编译/正确):
我相信你正在超越正则表达式的随意使用。您考虑过使用自己的解析器吗?语法非常简单,简单的语法分析是每个开发人员工具箱中都应该具备的技能。你能给出一个示例代码来完成这些操作吗??提前谢谢。向费马道歉:我找到了一个完美优雅的方式向你展示它;不幸的是,这篇评论的页边太小,无法容纳它。你能通过电子邮件发送吗;我不是你的个人代码编写服务。我最后的评论是一个笑话,如果你没有理解,我向你道歉。(如果你感兴趣的话,谷歌
Fermat的最后一个定理)我提交了一条评论,试图引导你朝着一个有用的方向前进。你想怎么做就怎么做。网上有很多开源解析工具和教程,我相信你
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;
}
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
}