Friday, 15 March 2013

c# - Extract value with a specific pattern -


I found it a while back, but I did not find it now.

I want to parse

  degrees: 44 minutes: 59 GPS coordinates to get an array of each value  "44 ° 59'59.93 \" N " 

I think the solution is a regedx or" {0} ° {1} '{2} \ A pattern like "{3}" should be, but I have no clue that which function is used to divide this GPS into the array.

You use the capture group to get those parts you are 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 require a degree in inset / doubles = m. Group [0]; Var minutes = M. group 1]; Var seconds = M. Group [2]; Various directions = M. Group [3]; }  

breaking it down:

  • ( .. ) point to a "group" Do - A part of the match that can be referred to as an adjacent section or substrings. (Unlike the {0} placeholder)
  • \ d will match one digit.
  • + means "one or more", thus \ d + means "one or more digits"
  • < Li> [ .. ] \ s means "white location" < Li> * means "zero or more"

You can add some additional S * s if any to catch the free space.


No comments:

Post a Comment