Skip to content

Instantly share code, notes, and snippets.

@moole
Created September 12, 2012 14:47
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moole/3707127 to your computer and use it in GitHub Desktop.
Save moole/3707127 to your computer and use it in GitHub Desktop.
GPS coordinate regular expression
FLOAT NUMBER \f
((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))
DEGREE SEPARATOR \ds
[^ms'′"″,\.\dNEWnew]
MINUTE SEPARATOR \ms
[^ds°"″,\.\dNEWnew]
SECOND SEPARATOR \ss
[^dm°'′,\.\dNEWnew]
ONE COORDINATE \c
\f(?:(?:\ds?)|(?:\ds+\f(?:(?:\ms?)|(?:\ms+\f\ss*))))
COMPLETE
([SNsn][\s]*)?\c([SNsn]?)[^\dSNsnEWew]+([EWew][\s]*)?\c([EWew]?)
([SNsn][\s]*)?((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ms'′"″,\.\dNEWnew]?)|(?:[^ms'′"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ds°"″,\.\dNEWnew]?)|(?:[^ds°"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))[^dm°'′,\.\dNEWnew]*))))([SNsn]?)[^\dSNsnEWew]+([EWew][\s]*)?((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ms'′"″,\.\dNEWnew]?)|(?:[^ms'′"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ds°"″,\.\dNEWnew]?)|(?:[^ds°"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))[^dm°'′,\.\dNEWnew]*))))([EWew]?)
WILL MATCH ALL OF
50°4'17.698"north, 14°24'2.826"east
50d4m17.698N 14d24m2.826E
40:26:46N,79:56:55W
40:26:46.302N 79:56:55.903W
49°59'56.948"N, 15°48'22.989"E
50d4m17.698N 14d24m2.826E
49.9991522N, 15.8063858E
N 49° 59.94913', E 15° 48.38315'
40°26′47″N 79°58′36″W
40d 26′ 47″ N 79d 58′ 36″ W
40.446195N 79.948862W
40,446195° 79,948862°
40° 26.7717, -79° 56.93172
40.446195, -79.948862
and more...
@ianengelbrecht
Copy link

I used http://www.regexr.com/ to test this. I used the string in line 19. Doesnt seem to work very well.

@tsamaya
Copy link

tsamaya commented Jun 9, 2016

Excellent !
it is working fine : https://regex101.com/r/gD9aU5/1

@lczech
Copy link

lczech commented Dec 8, 2017

Thanks, works (almost) great! One tiny correction: the second part does not recognize - chars before the number. This fix does:

([SNsn][\s]*)?((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ms'′"″,\.\dNEWnew]?)|(?:[^ms'′"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ds°"″,\.\dNEWnew]?)|(?:[^ds°"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))[^dm°'′,\.\dNEWnew]*))))([SNsn]?)[^\dSNsnEWew\+-]+([EWew][\s]*)?((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ms'′"″,\.\dNEWnew]?)|(?:[^ms'′"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ds°"″,\.\dNEWnew]?)|(?:[^ds°"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))[^dm°'′,\.\dNEWnew]*))))([EWew]?)

@ianengelbrecht
Copy link

ianengelbrecht commented Jul 27, 2018

For Javascript it does return the correct results for RegExp.test(), but not the correct values for the groups. For example:

//using Iczech's version
var exp = /([SNsn][\s]*)?((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ms'′"″,\.\dNEWnew]?)|(?:[^ms'′"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ds°"″,\.\dNEWnew]?)|(?:[^ds°"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))[^dm°'′,\.\dNEWnew]*))))([SNsn]?)[^\dSNsnEWew\+-]+([EWew][\s]*)?((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ms'′"″,\.\dNEWnew]?)|(?:[^ms'′"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))(?:(?:[^ds°"″,\.\dNEWnew]?)|(?:[^ds°"″,\.\dNEWnew]+((?:[\+-]?[0-9]*[\.,][0-9]+)|(?:[\+-]?[0-9]+))[^dm°'′,\.\dNEWnew]*))))([EWew]?)/g
exp.test(`40°26'46.302"S 79°56'55.90"W`) //return true

//BUT
`40°26'46.302"S 79°56'55.90"W`.match(exp) // returns [ '40°26', '46.302', 'S 79°56', '55.90' ]

Run in Node 8.11.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment