- Published on

# Regex for Latitude and Longitude

Latitude and longitude are geographic coordinates that are used to specify the location of a point on the Earth's surface. Latitude is a measure of how far a point is north or south of the equator, and is expressed in degrees from 0° at the equator to 90° at the North Pole and South Pole. Longitude is a measure of how far a point is east or west of the Prime Meridian, which is a line of longitude that passes through the Royal Observatory in Greenwich, England, and is expressed in degrees from 0° at the Prime Meridian to 180° at the international date line. In this article let's understand how we can create a regex for latitude & longitude and how regex can be matched for a given latitude & longitude.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of alphabets that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

# Structure of a Latitude and Longitude

The latitude and longitude should have the following criteria and structure-

- Latitude and Longitude are geographic coordinates that are used to specify the location of a point on the Earth's surface.
- Latitude is a measure of how far a point is north or south of the equator, and is expressed in degrees from 0° at the equator to 90° at the North Pole and South Pole.
- Longitude is a measure of how far a point is east or west of the Prime Meridian, which is a line of longitude that passes through the Royal Observatory in Greenwich, England, and is expressed in degrees from 0° at the Prime Meridian to 180° at the international date line.
- Together latitude and longitude can be used to specify a location on the Earth's surface.
- They are comma-separated and can be used to specify a location on the Earth's surface.

# Regex for checking if Latitude and Longitude is valid or not

Regular Expression for lowercase letter-

```
/^((\-?|\+?)?\d+(\.\d+)?),\s*((\-?|\+?)?\d+(\.\d+)?)$/gm
```

Test string examples for the above regex-

Input String | Match Output |
---|---|

sixty degree | does not match |

as,asd | does not match |

045, 180 | matches |

-90.000, -180.0 | matches |

47.1231231, 179.99999999 | matches |

Here is a detailed explanation of the above regex-

```
/^((\-?|\+?)?\d+(\.\d+)?),\s*((\-?|\+?)?\d+(\.\d+)?)$/gm
^ asserts position at start of a line
1st Capturing Group ((\-?|\+?)?\d+(\.\d+)?)
2nd Capturing Group (\-?|\+?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \-?
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Alternative \+?
\+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
3rd Capturing Group (\.\d+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
4th Capturing Group ((\-?|\+?)?\d+(\.\d+)?)
5th Capturing Group (\-?|\+?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \-?
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Alternative \+?
\+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
6th Capturing Group (\.\d+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
```

Hope this article was useful to check if the string is a valid Latitude and Longitude or not.