Created
September 12, 2012 14:47
-
-
Save moole/3707127 to your computer and use it in GitHub Desktop.
GPS coordinate regular expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
Excellent !
it is working fine : https://regex101.com/r/gD9aU5/1
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]?)
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
I used http://www.regexr.com/ to test this. I used the string in line 19. Doesnt seem to work very well.