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

时间:2013-03-23 00:30:30

标签: c# regex

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

我所拥有的字符串有这样的形式:

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)提取到坐标列表中。

这是Coordinates类:

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

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

所以我正在寻找一种更好的方法。也许整个逻辑只使用正则表达式或任何更清洁的方式来做它。

由于

2 个答案:

答案 0 :(得分: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"]));
}

答案 1 :(得分:0)

使用组捕获实际数字。另外,使用@“”表示法来减少反斜杠。

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

查看Match.Groups