这是一个“有点”不错的片段显示:
- 预编译的正则表达式
- LINQ 到匿名类型投影
- 文化感知(正确)数字解析和打印
您可能希望在现实生活中提取某些代码(例如数字解析)。
在 Ideone.com上现场观看。
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Globalization;
namespace SODemo
{
class MainClass
{
private static readonly CultureInfo CInfo = CultureInfo.CreateSpecificCulture("en-US");
public static void Main (string[] args)
{
string segment = "51.54398, -0.27585;51.55175, -0.29631;51.56233, -0.30369;51.57035, -0.30856;51.58157, -0.31672;51.59233, -0.3354";
var re = new Regex(@"\s*(?<lat>[-+]?[0-9.]+),\s*(?<lon>[-+]?[0-9.]+)\s*;", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
var locations = re.Matches(segment).Cast<Match>().Select(m => new
{
Lat = decimal.Parse(m.Groups["lat"].Value, CInfo),
Long = decimal.Parse(m.Groups["lon"].Value, CInfo),
});
foreach (var l in locations)
Console.WriteLine(l);
}
}
}
输出:
{ Lat = 51,54398, Long = -0,27585 }
{ Lat = 51,55175, Long = -0,29631 }
{ Lat = 51,56233, Long = -0,30369 }
{ Lat = 51,57035, Long = -0,30856 }
{ Lat = 51,58157, Long = -0,31672 }