使用正则表达式从字符串中提取坐标的更好方法

本文关键字:坐标 更好 方法 提取 正则表达式 字符串 | 更新日期: 2023-09-27 18:05:19

我想从字符串中提取坐标。

字符串的形式是:

string Coord = @"
      at point  X=-277923.7300  Y=16462.7700  Z=   0.0000
      at point  X=-277816.6200  Y=16311.1500  Z=   0.0000
      at point  X=-277629.1900  Y=16109.7100  Z=   0.0000
      at point  X=-277573.4000  Y=16055.5600  Z=   0.0000
      at point  X=-277524.3100  Y=16023.0700  Z=   0.0000
      at point  X=-277508.6900  Y=15986.2400  Z=   0.0000
      at point  X=-277488.6100  Y=15866.0200  Z=   0.0000
      at point  X=-277466.3000  Y=15766.3800  Z=   0.0000
      at point  X=-277421.6700  Y=15710.0700  Z=   0.0000
      at point  X=-277281.0900  Y=15595.2700  Z=   0.0000
      at point  X=-277234.2300  Y=15547.6100  Z=   0.0000
      at point  X=-277185.1400  Y=15469.6400  Z=   0.0000
      at point  X=-277091.4900  Y=15354.1300  Z=   0.0000:;

我想提取所有的X和Y(不关心Z)到一个坐标列表。

这是坐标类:

public class Coordinates
{
    public double Longitude { get; set; }
    public double Latitude { get; set; }
    public Coordinates(double Long, double Lat)
    {
        this.Longitude = Long;
        this.Latitude = Lat;
    }
}

我被推荐使用Regex,因为我是一个完全的新手,我挣扎了一下,但设法准备了一些东西。

这是我目前所做的:

private List<Coordinates> ExtractCoordinates(string Coordinates)
    {
        List<Coordinates> lstOfCoordinates = new List<Coordinates>();
        //I managed to put this regex together after some trial and error
        //This regex will extract this pattern : " X=(any decimal) Y=(any decimal)"
        Regex reg = new Regex("(X=)-?''d+''.?''d+''s+(Y=)-?''d+''.?''d+");
        //I get the matches and save them in a list
        MatchCollection collection = reg.Matches(Coord);
        List<string> lstOfMatches = (from Match match in collection
                                     select match.Value).ToList();
        //At this point I have alist of string having this form: "X=-277923.7300  Y=16462.7700" 
        //I had no idea how to proceed from here so I did it in a bad way
        //Basically I just Cut the string when I detect the first '=' 
        //and then take the rest until the Y. I remove the = and trim it 
        //and then parse it to double this is the X
        //Same logic for the Y coordinates
        foreach (string match in lstOfMatches)
        {
            double X = double.Parse(match.Substring(match.IndexOf('='), match.IndexOf('Y') - match.IndexOf('=')).Replace("=","").Trim());
            double Y = double.Parse(match.Substring(match.IndexOf("Y=")).Replace("Y=", "").Trim());
            lstOfCoordinates.Add(new Coordinates(X, Y));
        }
        return lstOfCoordinates;
    }

基本上它是有效的,但我觉得这不是一个好方法。

所以我在寻找一个更好的方法来做这件事。也许只使用Regex或任何更干净的方法的建议来完成整个逻辑。

谢谢

使用正则表达式从字符串中提取坐标的更好方法

var results = Regex.Matches(Coord, @"X=(?<X>-?'d+.?'d+)'s+Y=(?<Y>'d+.?'d+)");
for (int i = 0; i < results.Count; i++)
{
    Console.WriteLine(string.Format("X={0} Y={1}", results[i].Groups["X"], results[i].Groups["Y"]));
}

使用组来捕获实际的数字。另外,使用@"符号来减少反斜杠。

Regex reg = new Regex(@"(X=)-?('d+('.'d+)?)'s+(Y=)-?('d+('.'d+)?)");

看Match。组