Regex for Latitude/Longitude
Regex Pattern
^-?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?),\s*-?(?:1[0-7]\d(?:\.\d+)?|180(?:\.0+)?|\d{1,2}(?:\.\d+)?)$Geographic coordinates (lat, lng) — lat -90 to 90, lng -180 to 180
Quick Answer
The regex pattern for latitude/longitude is `^-?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?),\s*-?(?:1[0-7]\d(?:\.\d+)?|180(?:\.0+)?|\d{1,2}(?:\.\d+)?)$`. Geographic coordinates (lat, lng) — lat -90 to 90, lng -180 to 180. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 40.7128, -74.0060 | ✓ Matches |
| 0, 0 | ✓ Matches |
| -33.8688, 151.2093 | ✓ Matches |
| 91, 0 | ✗ No match |
| 0, 181 | ✗ No match |
| not coords | ✗ No match |
Code Examples
javascript
const regex = /^-?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?),\s*-?(?:1[0-7]\d(?:\.\d+)?|180(?:\.0+)?|\d{1,2}(?:\.\d+)?)$/;
const isValid = regex.test(value);python
import re
pattern = r'^-?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?),\s*-?(?:1[0-7]\d(?:\.\d+)?|180(?:\.0+)?|\d{1,2}(?:\.\d+)?)$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^-?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?),\s*-?(?:1[0-7]\d(?:\.\d+)?|180(?:\.0+)?|\d{1,2}(?:\.\d+)?)$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^-?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?),\s*-?(?:1[0-7]\d(?:\.\d+)?|180(?:\.0+)?|\d{1,2}(?:\.\d+)?)$/', $value)) {
echo "valid";
}java
String pattern = "^-?(?:[1-8]?\\d(?:\\.\\d+)?|90(?:\\.0+)?),\\s*-?(?:1[0-7]\\d(?:\\.\\d+)?|180(?:\\.0+)?|\\d{1,2}(?:\\.\\d+)?)$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
Positive Integer
Integers greater than zero (no leading zeros)
Non-negative Integer
Zero or positive integer
Decimal Number
Optionally signed decimal number
Hex Color Code
3 or 6-digit hex color with optional #
USD Currency
US dollar amount with optional $ and thousands separators
Hex Color with Alpha
Hex color code with optional alpha channel (3, 4, 6, or 8 digits)