2

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

我拥有的字符串具有这种形式:

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;
    }

好吧,基本上它可以工作,但我觉得这不是一个好方法。

所以我正在寻找一种更好的方法来做到这一点。也许只使用正则表达式或任何关于更清洁方式的建议来完成整个逻辑。

谢谢

4

2 回答 2

1
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"]));
}
于 2013-03-23T00:56:27.563 回答
0

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

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

查看 Match.Groups

于 2013-03-23T00:43:28.670 回答