我有一个我想要提取GPS坐标的HTML文件,我试图通过创建一个regEx来做到这一点,但到目前为止没有运气。
我正在使用C#来解析HTML文件
以下是应提取的GPS数据示例。
S 33 58.254 E 023 53.269
任何帮助将不胜感激。
这是指定GPS坐标的样本文本
<span style="text-decoration: underline;">TOURIST INFORMATION</span><br>
Tourism Office <span style="font-style: italic;">(S 33 58.254 E 023
53.269, Gammasi St, 042-281-1098,)
我只需要提取S 33 58.254 E 023 53.269
答案 0 :(得分:2)
以下是C#中的一个示例,以防您想要解析GPS,而不仅仅是从HTML代码中提取它:
var text = @"Some example that contains S 33 58.254 E 023 53.269
and also S 22 58.123 W 021 53.2";
var pattern = @"([SN])\s(\d+)\s(\d+(?:\.\d+)?)\s([EW])\s(\d+)\s(\d+(?:\.\d*)?)";
var m = Regex.Matches(text, pattern);
for (int i = 0; i < m.Count; i++) {
Console.WriteLine("GPS Found: {0}", m[i].Value);
Console.WriteLine("-----");
Console.WriteLine(m[i].Groups[1].Value);
Console.WriteLine(m[i].Groups[2].Value);
Console.WriteLine(m[i].Groups[3].Value);
Console.WriteLine(m[i].Groups[4].Value);
Console.WriteLine(m[i].Groups[5].Value);
Console.WriteLine(m[i].Groups[6].Value);
Console.WriteLine("-----");
}
以上示例将打印:
GPS Found: S 33 58.254 E 023 53.269
-----
S
33
58.254
E
023
53.269
-----
GPS Found: S 22 58.123 W 021 53.2
-----
S
22
58.123
W
021
53.2
-----
修改强>
我真的不知道像53.2
这样的值是否可以是整数,例如53
,但是为了以防万一,我还有这个值。
答案 1 :(得分:1)
这是一个基本的匹配,只需稍微玩一下就可以获得更好的结果:
[SN]\s\d+\s\d+\.\d+\s[EW]\s\d+\s\d+\.\d+
答案 2 :(得分:0)
我首先要研究“可能性”。我会写
[SN]\s-?\d{1,3]\s\d+\.\d+\s[EW]\s-?\d{1,3]\s\d+\.\d+
这只是一个好朋友!
答案 3 :(得分:0)
如果您的数据模式是常量,我的意思是跨度样式部分(< span style="font-style: italic;" >
)
为什么不试试GetStringBetween
italic;">(
和,
以下是我为我使用的方法:
public static string GetStringBetween(string input, int searchStartIndex, string startMarker, string endMarker, out int foundAtIndex)
{
foundAtIndex = -1;
if (input == null)
return null;
int st = searchStartIndex;
int startIndex = input.IndexOf(startMarker, st);
if (startIndex < 0)
return null;
int endIndex = input.IndexOf(endMarker, startIndex + startMarker.Length);
if (endIndex < 0)
return null;
int occurenceIndex = startIndex + startMarker.Length;
string data = input.Substring(occurenceIndex, endIndex - occurenceIndex);
foundAtIndex = occurenceIndex;
return data;
}
从searchStartIndex = 0
开始,然后继续使用之前调用的新searchStartIndex = foundAtIndex
循环调用它。通过这种方式,您可以从完整档案中获取所有感兴趣的GPS字符串。