Skip to content

Instantly share code, notes, and snippets.

@myh-st
Forked from debkanchan/regex.md
Created October 18, 2022 05:26
Show Gist options
  • Save myh-st/08c7e79466e8daa21ac9c4293ee7763f to your computer and use it in GitHub Desktop.
Save myh-st/08c7e79466e8daa21ac9c4293ee7763f to your computer and use it in GitHub Desktop.
RegEx for Latitude and Longitude

Regular Expression(RegExp) for Latitude and Longitude

Just Latitude:

^-?([0-8]?[0-9]|90)(\.[0-9]{1,10})$

matches:

  • 56.3847
  • -56.387

unmatches:

  • 3
  • 30
  • 300
  • 91.34456
  • 40.238946234652374238746

Just Longitude or Both (Because lat's range is a subset of long)

^-?([0-9]{1,2}|1[0-7][0-9]|180)(\.[0-9]{1,10})$

matches:

  • 56.3847
  • -56.387
  • 91.34456
  • 127.485784
  • -130.37567

unmatches:

  • 3
  • 30
  • 300
  • 40.238946234652374238746
  • 181.394898

Just Latitude with optional decimal numbers:

^-?([0-8]?[0-9]|90)(\.[0-9]{1,10})?$

matches:

  • 3
  • 30
  • 56.3847
  • -56.387

unmatches:

  • 300
  • 91.34456
  • 40.238946234652374238746

Just Longitude or Both with optional decimal numbers (Because lat's range is a subset of long)

^-?([0-9]{1,2}|1[0-7][0-9]|180)(\.[0-9]{1,10})?$

matches:

  • 3
  • 30
  • 56.3847
  • -56.387
  • 91.34456
  • 127.485784
  • -130.37567

unmatches:

  • 300
  • 40.238946234652374238746
  • 181.394898

Note: This RegEx matches to coordinates upto 10 decimal places accurate. To increase or decrease the accuracy change the 10 to your desired accuracy in the last {1,10}.

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