Python Forum
trying to recall a regex for re.split()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to recall a regex for re.split()
#2
The following seems to work; I've tried to be exhaustive (but a simplier way should exist)
data = '144mhz'
Number, Units = re.search(r"([+\-]?\d+\.\d+[eE]?[+\-]?[\d]?[\d]?|[+\-]?\d+[eE]?[+\-]?[\d]?[\d]?)[\s+]?([a-z]+)", data.lower()).groups()
With :
  • [+\-]? => if a sign is encountered (if comes from the "?")
  • \d+\.\d+ => for floats (the "+" indicates one or more occurences)
  • [eE]?[+\-]?[\d]?[\d]? => for scientific notation with or without the sign, and with 2 digits max here
  • | => means "or"
  • [+\-]?\d+[eE]?[+\-]?[\d]?[\d]?)[\s+]? => same thing for intergers (the dot followed by "\d+" have been removed from the previous sentence)
  • [[\s+]?=> if there's any space
  • [a-z]+ => to get strings
  • remember that parentheses indicate what you want to recover
Reply


Messages In This Thread
RE: trying to recall a regex for re.split() - by paul18fr - May-18-2022, 12:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split pdf in pypdf based upon file regex standenman 1 2,307 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  recall cool_person 1 1,106 May-07-2022, 08:04 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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