【问题标题】:Regex to extract GPS coordinates C#正则表达式提取 GPS 坐标 C#
【发布时间】:2011-05-28 07:55:19
【问题描述】:

我有一个 HTML 文件,其中包含我想要提取的 GPS 坐标,我试图通过创建一个正则表达式来做到这一点,但到目前为止还没有运气。

我正在使用 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

【问题讨论】:

  • 你为什么使用 RegEx 而不是更简单的string.Split
  • @Oded 我不认为string.Split 是一个好的选择。他想提取 GPS 代码,而不是解析 GPS 代码本身。
  • @Oscar - 他的编辑澄清了这一点。之前还不清楚。

标签: c# regex


【解决方案1】:

这是一个 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,但为了以防万一,我已经算了。

【讨论】:

    【解决方案2】:

    这是一个基本的比赛,只是稍微玩一下以获得更好的结果:

    [SN]\s\d+\s\d+\.\d+\s[EW]\s\d+\s\d+\.\d+
    

    【讨论】:

    • 空格是\s(小写的's'),而不是\S
    【解决方案3】:

    我首先要检查“可能性”。我会写

    [SN]\s-?\d{1,3]\s\d+\.\d+\s[EW]\s-?\d{1,3]\s\d+\.\d+

    这只是一个口水球好运!

    【讨论】:

      【解决方案4】:

      如果您的数据模式是恒定的,我的意思是跨度样式部分 (&lt; span style="font-style: italic;" &gt;)

      为什么不试试GetStringBetweenitalic;"&gt;(,

      下面是我写的供我使用的方法:

      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 字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 2015-09-04
        • 1970-01-01
        相关资源
        最近更新 更多