Reputation: 87
I found this some time ago but I can't find it now.
I want to parse GPS coordinates having the format "44°59'59.93\" N "
in C# to get an array of every value.
Ex:
degree:44
minutes:59
seconds:59.93
direction:"N"
I think the solution must be like a regex or a pattern like "{0}°{1}'{2}\"{3}", but I have no clue what function to use for splitting this GPS coordinate into an array.
Upvotes: 0
Views: 152
Reputation: 841
You can use capture groups to get the parts you're asking about.
string value = @"44°59'59.93"" N";
var match = Regex.Match(value, @"(\d+)°(\d+)'([\d\.]+"")\s*([NSEW])");
if (m.Success) {
//note that these are all strings and will need to be parsed into ints/doubles
var degrees = m.Groups[0];
var minutes = m.Groups[1];
var seconds = m.Groups[2];
var direction = m.Groups[3];
}
Breaking this down:
(
..)
signify a "group"--part of the match that can be referred to as a contiguous chunk or substring. (like the opposite of {0}
placeholders)\d
will match a digit.+
means "one or more", thus \d+
means "one or more digits"[
..]
signify a choice of any of the things in the brackets (for the simple case)\s
means "whitespace"*
means "zero or more". Thus, \s*
means "there might be some spaces in here, go ahead and take them"You may want to add some extra \s*
s in there to catch spaces if there are any.
Upvotes: 0
Reputation: 174706
Split your input according to the below regex.
string[] lines = Regex.Split(value, @"[°'""] *");
Then get the degree from index 0, minutes from index 1, seconds from index 2, Direction from index 3.
Upvotes: 3