gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:15 24 4
gpt4 key购买 nike

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

我的字符串有这种形式:

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"]));
}

关于c# - 使用正则表达式从字符串中提取坐标的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15581938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com