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
#3
Maybe like this:

import regex
 
coords = '45° 46\' 52" N 108° 30\' 14" W'
# simple it up a bit, all those backstrokes are confusing
string = coords.replace('°', ' degrees').replace('\'', ' minutes').replace('"', ' seconds') 
# regex for string 
e = regex.compile(r'(\d+)\s+degrees\s+(\d+)\s+minutes\s+(\d+)\s+seconds\s+([NSWE])\s+(\d+)\s+degrees\s+(\d+)\s+minutes\s+(\d+)\s+seconds\s+([NSWE])') 
# find what you want
res = e.match(string)
for e in res.groups():
    print(e)
Output:
45 46 52 N 108 30 14 W
I believe you can do the arithmetic.
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,595 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