User:TomTheHand/Unit tests for AWB regexes/Length

This section contains regular expressions for formatting units of length.

Feet and inches

edit

Remove commas

edit
Description
Remove commas from quantities of feet or inches. If they're in x'y" format, I'm not worrying about trying to figure out if they represent minutes or seconds of arc because then there couldn't possibly be more than sixty, and so there won't be any commas.
Find
\b(?<=\d+)(\s| |,)(?=\d{3,3}(?:(?:\s| |,)\d{3,3})*(\.\d*)?(?:\s| |-)*(?:'|’|′|"|”|″|f(?:ee|oo)?t\b|in(?:ch(?:es)?)?\b))
Replace with
(leave this field blank!)
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15,000'
  2. 150,000’
  3. 1,500.8′
  4. 15,000"
  5. 150,000”
  6. 1,500.8″
  7. 11,000' (3,300 km)
  8. 5,280 feet
  9. 1,800 in
  1. 15000'
  2. 150000’
  3. 1500.8′
  4. 15000"
  5. 150000”
  6. 1500.8″
  7. 11000' (3,300 m)
  8. 5280 feet
  9. 1800 in

Convert feet/inches

edit
Description
Replace abbreviated lengths in feet/inches with {{convert}}
Find
(?<!°(?:\s|&nbsp;)*)(?<!,)\b(\d+)(?:\s|&nbsp;)*(?:ft\b|['’′])(?:\s|&nbsp;)*((?:1[01]|\d)(?:\.\d+)?)(?:\s|&nbsp;)*(?:(?(2)in\b|["”″]))((?:\s|&nbsp;)*\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s|&nbsp;)*(?:k(?:ilo)?|c(?:enti)?)?m(?:eter?|etre?)?s?\b\)?)?
Replace with
{{convert|$1|ft|$2|in|abbr=on}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 6'1"
  2. 22' 11"
  3. 105' 8.75"
  4. 6’1”
  5. 22′ 11″
  6. 6'1" (180cm)
  7. 22' 11" (7.0 m)
  8. 105' 8.75" (32.2 m)
  9. 6’1” (1.8m)
  10. 22′ 11″ (7.0 m)
  11. 6 ft 1 in
  12. 22 ft 11 in
  1. 6 ft 1 in (1.85 m)
  2. 22 ft 11 in (6.99 m)
  3. 105 ft 8.75 in (32.2263 m)
  4. 6 ft 1 in (1.85 m)
  5. 22 ft 11 in (6.99 m)
  6. 6 ft 1 in (1.85 m)
  7. 22 ft 11 in (6.99 m)
  8. 105 ft 8.75 in (32.2263 m)
  9. 6 ft 1 in (1.85 m)
  10. 22 ft 11 in (6.99 m)
  11. 6 ft 1 in (1.85 m)
  12. 22 ft 11 in (6.99 m)
Text this regex should not modify:
  1. 6'13"
  2. 51° 5' 11" N 0° 7' 9" W
  3. 51°5'11" N 0°7'9" W
  4. 51° 5' 11"N 0° 7' 9"W
  5. 6' 1 in
  6. 6 ft 1"

Convert x'

edit
Description
Replace lengths in feet/inches in x' format with {{convert}}
Find
\b(?<!°(?:\s| )*)(\d+)['’′](\s?\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s| )*(?:k(?:ilo)?|c(?:enti)?)?(?:m(?:eters?|etres?)?)\b)\)?)?(?!(\d+)["”″])
Replace with
{{convert|$1|ft|abbr=on}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 6'
  2. 22'
  3. 105'
  4. 6’
  5. 22′
  6. 6' (180cm)
  7. 22' (7.0 m)
  8. 105' (32.0 m)
  9. 6’ (1.8m)
  10. 22′ (7.0 m)
  1. 6 ft (1.8 m)
  2. 22 ft (6.7 m)
  3. 105 ft (32 m)
  4. 6 ft (1.8 m)
  5. 22 ft (6.7 m)
  6. 6 ft (1.8 m)
  7. 22 ft (6.7 m)
  8. 105 ft (32 m)
  9. 6 ft (1.8 m)
  10. 22 ft (6.7 m)
Text this regex should not modify:
  1. 51° 5' 11" N 0° 7' 9" W
  2. 51°5'11" N 0°7'9" W
  3. 51° 5' 11"N 0° 7' 9"W
  4. 6'1"
  5. 22' 11"
  6. 105' 8.75"

Replace miles with {{convert}}

edit

Remove commas

edit
Description
Remove commas from quantities of miles.
Find
\b(?<=\d+)(\s| |,)(?=\d{3,3}(?:(?:\s| |,)\d{3,3})*(\.\d*)?(?:\s| |-)?(?:mi(?:les?)?)\b)
Replace with
(leave this field blank!)
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15,000 mi
  2. 150,000 mi
  3. 1,500.8 miles
  4. 11,000 miles (20,000 km)
  1. 15000 mi
  2. 150000 mi
  3. 1500.8 miles
  4. 11000 miles (20,000 km)

Convert abbreviated noun phrases

edit
Description
Replace abbreviated mile noun phrases with {{convert}}.
Find
\b(?<![,\.$])(\d+(?:\.\d+)?)(?:\s| |-)?mi\b(\s?\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s| )?(?:(?:k(?:ilo)?)?(?:m(?:eters?|etres?)?)\b)\)?)?
Replace with
{{convert|$1|mi|abbr=on}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15-mi
  2. 15000 mi
  3. 150000 mi
  4. 1500.8 mi
  5. 1 mi
  6. 11000 mi (20,000 km)
  1. 15 mi (24 km)
  2. 15,000 mi (24,000 km)
  3. 150,000 mi (240,000 km)
  4. 1,500.8 mi (2,415.3 km)
  5. 11,000 mi (18,000 km)
Text this regex should not modify:
  1. 12-mile

Convert unabbreviated noun phrases

edit
Description
Replace unabbreviated mile noun phrases with {{convert}}.
Find
\b(?<![,\.$])(\d+(?:\.\d+)?)(?:\s| )?miles?\b(\s?\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s| )?(?:(?:k(?:ilo)?)?(?:m(?:eters?|etres?)?)\b)\)?)?
Replace with
{{convert|$1|mi}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15 miles
  2. 15000 miles
  3. 150000 miles
  4. 1500.8 miles
  5. 1 mile
  6. 11000 miles (20,000 km)
  1. 15 miles (24 km)
  2. 15,000 miles (24,000 km)
  3. 150,000 miles (240,000 km)
  4. 1,500.8 miles (2,415.3 km)
  5. 11,000 miles (18,000 km)
Text this regex should not modify:
  1. 12-mile

Convert adjectival phrases

edit
Description
Replace mile adjectival phrases with {{convert}}.
Find
\b(?<![,\.$])(\d+(?:\.\d+)?)-miles?\b(\s?\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s| )?(?:\s| )?(?:(?:k(?:ilo)?)?(?:m(?:eters?|etres?)?)\b)\)?)?
Replace with
{{convert|$1|mi|adj=on}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15-mile
  2. 15000-mile
  3. 150000-mile
  4. 1500.8-mile
  5. 1-mile
  6. 11000-mile (20,000 km)
  1. 15-mile (24 km)
  2. 15,000-mile (24,000 km)
  3. 150,000-mile (240,000 km)
  4. 1,500.8-mile (2,415.3 km)
  5. 11,000-mile (18,000 km)
Text this regex should not modify:
  1. 12 miles
  2. 150 mi

Replace nautical miles with {{convert}}

edit

This task requires three rules. The first strips commas and spaces from large numbers of nautical miles. The second converts standard uses of nautical miles. The third converts adjectival uses of nautical miles. They should not be set up as sub-rules, because then they would only run when we've stripped commas or spaces; we may be dealing with small numbers, so there were no commas to strip, or the article may have simply been written without commas. I don't abbreviate nautical miles, because the symbol to use is disputed, and I feel better writing the unit out than I do using a symbol I don't agree with.

I had originally written just one rule, but I couldn't figure out how to support a number of any length; I had to include separate captures of each group of digits. I set it up to capture the equivalent of a fifth of a light-year, but then I decided that there has to be a way to deal with any number of commas. This is what I came up with.

Remove commas

edit
Description
Remove commas from quantities of abbreviated nautical miles. This recognizes "nm" as an abbreviation for nautical miles. Yes, that is the official SI symbol for nanometers. I work primarily on articles about ships, mostly from World War II and before, so I don't run into nanometers very much. I often run into "nm" as an abbreviation for nautical miles. If you see a lot of nanometers, modify this regex, be careful with it, or just don't use it.
Find
\b(?<=\d+)(\s|&nbsp;|,)(?=\d{3,3}(?:(?:\s|&nbsp;|,)\d{3,3})*(\.\d*)?(?:\s|&nbsp;|-)?(?:nautical(\s|&nbsp;|-)miles?|nmi?)\b)
Replace with
(leave this field blank!)
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15,000 nm
  2. 150,000 nmi
  3. 1,500.8 nm
  4. 11,000 nm (20,000 km)
  1. 15000 nm
  2. 150000 nmi
  3. 1500.8 nm
  4. 11000 nm (20,000 km)

Convert noun phrases

edit
Description
Replace nautical mile noun phrases with {{convert}}.
Find
\b(?<![,\.])(\d+(?:\.\d+)?)(?:\s| )?(?:nautical(\s| |-)miles?|-?nmi?)(\s?\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s| )?(?:kilometers|kilometres|km))?
Replace with
{{convert|$1|nmi|km}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15-nm
  2. 15000 nm
  3. 150000 nmi
  4. 1500.8 nautical miles
  5. 1 nautical mile
  6. 11000 nm (20,000 km)
  1. 15 nautical miles (28 km)
  2. 15,000 nautical miles (28,000 km)
  3. 150,000 nautical miles (280,000 km)
  4. 1,500.8 nautical miles (2,779.5 km)
  5. 11,000 nautical miles (20,000 km)
Text this regex should not modify:
  1. 12-nautical-mile

Convert adjectival phrases

edit
Description
Replace nautical mile adjectival phrases with {{convert}}.
Find
\b(?<![,\.])(\d+(?:\.\d+)?)-(?:nautical(\s| |-)miles?)(\s?\(?\d{1,3}(?:,?\d{3,3})*(\.\d*)?(?:\s| |-)?(?:kilometers?|kilometres?|km))?
Replace with
{{convert|$1|nmi|km|adj=on}}
Regular expression? Case sensitive?
Y N
Text this regex should modify: Intended result:
  1. 15-nautical-mile
  2. 15000-nautical-mile
  3. 150000-nautical-mile
  4. 1500.8-nautical-mile
  5. 1 nautical mile
  6. 11000-nautical-mile (20,000 km)
  1. 15-nautical-mile (28 km)
  2. 15,000-nautical-mile (28,000 km)
  3. 150,000-nautical-mile (280,000 km)
  4. 1,500.8-nautical-mile (2,779.5 km)
  5. 11,000-nautical-mile (20,000 km)
Text this regex should not modify:
  1. 12 nautical miles
  2. 150 nm