Jump to content

User:TomTheHand/Unit tests for AWB regexes/Geographical coordinates

From Wikipedia, the free encyclopedia

This section contains regular expressions for converting plain-text geographical coordinates to {{coord}}.

I had originally hoped to be able to combine all of this functionality into one gigantic, extremely awesome expression. I'm not sure if it's possible, but I've definitely given up. These do make sure that you enter valid coordinates, which I'm pleased with.

I originally just had them require the correct number of digits (up to two for latitude and up to three for longitude), but then you could still input something like 91° N, 181° W. Fixing that problem made me feel sufficiently spiffy that I could give up on my gigantic regex dream without much guilt.

Convert coordinates in degree format

[edit]
Description
Use {{coord}} on coordinates in degree format.
Find
\b(?<!\.)(90|[1-8]\d(?:\.\d+)?)°(?:\s|&nbsp;)*(N|S),?(?:\s|&nbsp;)*(180|(?:1[1-7]\d|\d{1,2})(?:\.\d+)?)°(?:\s|&nbsp;)*(W|E)\b
Replace with
{{coord|$1|$2|$3|$4}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 51°N 0°W
  2. 40.71° N, 74.0° W
  1. 51°N 0°W / 51°N -0°E / 51; -0
  2. 40°43′N 74°00′W / 40.71°N 74.0°W / 40.71; -74.0
Text this regex should not modify:
  1. 91°N 74°W
  2. 90.1° N, 74.0° W
  3. 51°N 181°W
  4. 51° N, 180.1° W

Convert coordinates in fully-decimal degree format

[edit]
Description
Use {{coord}} on coordinates in fully-decimal degree format
Find
(?<!\.)(-)?\b(90|[1-8]\d(?:\.\d+)?)°,?(?:\s|&nbsp;)*(-)?(180|(?:1[1-7]\d|\d{1,2})(?:\.\d+)?)°
Replace with
{{coord|$1$2|$3$4}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 40.71°, -74.0°
  2. -40.71°, 74.0°
  1. 40°43′N 74°00′W / 40.71°N 74.0°W / 40.71; -74.0
  2. 40°43′S 74°00′E / 40.71°S 74.0°E / -40.71; 74.0
Text this regex should not modify:
  1. 91° -74°
  2. -90.1°, 74.0°
  3. 51° -181°
  4. -51°, 180.1°

Convert coordinates in degrees-minutes format

[edit]
Description
Use {{coord}} on coordinates in degrees-minutes format
Find
\b(?<!\.)([1-8]\d)°(?:\s|&nbsp;)*([1-5]?\d)(\.\d+)?['’′](?:\s|&nbsp;)*(N|S),?(?:\s|&nbsp;)*((?:1[1-7]\d|\d{1,2})(?:\.\d+)?)°(?:\s|&nbsp;)*([1-5]?\d)(\.\d+)?['’′](?:\s|&nbsp;)*(W|E)\b
Replace with
{{coord|$1|$2$3|$4|$5|$6$7|$8}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 51°5'N 0°7'W
  2. 40° 19.3′N, 74° 50.1234′ W
  1. 51°5′N 0°7′W / 51.083°N 0.117°W / 51.083; -0.117
  2. 40°19.3′N 74°50.1234′W / 40.3217°N 74.8353900°W / 40.3217; -74.8353900
Text this regex should not modify:
  1. 51°N 0°W
  2. 40.71°, -74.0°
  3. 90° 3' N, 74° 19' W
  4. 51° 3' N, 180° 19' W
  5. 51°61'N 0°7'W
  6. 40° 19.3′N, 74° 60.1234′ W

Convert coordinates in degrees-minutes-seconds format

[edit]
Description
Use {{coord}} on coordinates in degrees-minutes-seconds format
Find
\b(?<!\.)([1-8]\d)°(?:\s|&nbsp;)*([1-5]?\d)['’′](?:\s|&nbsp;)*([1-5]?\d)(\.\d+)?["”″](?:\s|&nbsp;)*(N|S),?(?:\s|&nbsp;)*((?:1[1-7]\d|\d{1,2})(?:\.\d+)?)°(?:\s|&nbsp;)*([1-5]?\d)['’′](?:\s|&nbsp;)*([1-5]?\d)(\.\d+)?["”″](?:\s|&nbsp;)*(W|E)\b
Replace with
{{coord|$1|$2|$3$4|$5|$6|$7|$8$9|$10}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 51°5'11"N 0°7'19"W
  2. 40° 44' 19.3"N, 74° 16' 50.1234" W
  1. 51°5′11″N 0°7′19″W / 51.08639°N 0.12194°W / 51.08639; -0.12194
  2. 40°44′19.3″N 74°16′50.1234″W / 40.738694°N 74.280589833°W / 40.738694; -74.280589833
Text this regex should not modify:
  1. 51°N 0°W
  2. 40.71°, -74.0°
  3. 40° 19.3′N, 74° 50.1234′ W
  4. 51°16'61"N 0°7'33"W
  5. 40° 46' 19.3"N, 74° 11' 60.1234" W