Here are some nice snippets:
- precompiled regexen
- LINQ to anonymous type projection
- Parsing and printing culture-supporting numbers ( correct )
You want to extract certain code (for example, parsing numbers) in real life.
Watch live on 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); } } }
Output:
{ 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 }
sehe
source share