Python Forum
[SOLVED] Can't figure out right regex
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Can't figure out right regex
#1
Question 
Hello,

I need to convert GPS coordinates from DMS to DD.

Provided there's no even better way, what's the right regex to 1) find the orientation (N, S, W, E) and 2) keep in memory using brackets?

Thank you.

#From Degrees, minutes, and seconds (DMS) to Decimal degrees (DD)

import re

coords = "45° 46' 52\" N 108° 30' 14\" W"

coords = coords.replace(" ","") #remove spaces -> 45°46'52"N108°30'14"W

coords = re.match(r"(\d+)°(\d+)'(.+?)\"([NSWE])",coords)
coords = re.match(r"(\d+)°(\d+)'(.+?)\"[(NSWE)]",coords)
coords = re.match(r"(\d+)°(\d+)'(.+?)\"([NSWE].)",coords)
coords = re.match(r"(\d+)°(\d+)'(.+?)\"[(NSWE).]",coords)
if coords:
	#OK print(f"{coords.group(0)},{coords.group(1)},{coords.group(2)},{coords.group(3)}")

	#4: IndexError: no such group
	print(f"{coords.group(1)},{coords.group(2)},{coords.group(3)},{coords.group(4)}")
likes this post
Reply


Messages In This Thread
[SOLVED] Can't figure out right regex - by Winfried - Mar-02-2025, 01:35 PM
RE: Can't figure out right regex - by snippsat - Mar-02-2025, 05:24 PM
RE: Can't figure out right regex - by Pedroski55 - Mar-02-2025, 05:34 PM
RE: Can't figure out right regex - by Winfried - Mar-02-2025, 05:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 4,375 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  Help with a regex? (solved) wrybread 3 1,593 May-01-2023, 05:12 AM
Last Post: deanhystad
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 2,187 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 3,783 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  [SOLVED] Why does regex fail cleaning line? Winfried 5 3,614 Aug-22-2021, 06:59 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020