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は気にしない)を座標のリストに抽出したいと思います。

これは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;
    }

基本的には機能しますが、それを行うには良い方法ではないと思います。

だから私はそれを行うためのより良い方法を探しています。たぶん、正規表現またはそれを行うためのよりクリーンな方法に関する提案のみを使用して、ロジック全体を実行します。

ありがとう

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 に答える