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()
#7
like this:
def numunits(s=None):
    if not isinstance(s,str):
        raise TypeError('string expected')
    i = iter(range(len(s)))
    for p in i:
        try:
            v = float(s[:p])
            break
        except:
            continue
    else:
        raise ValueError('no number')
    for p in i:
        try:
            v = float(s[:p])
            continue
        except:
            break
    else:
        raise ValueError('no units')
    if s[p] != ' ':
        p -= 1
    return v,s[p:]
if __name__ == '__main__':
    a = ['144mHz','432 mHz','1.296GHz','2.304 GHz']
    for x in a:
        print(repr(x))
        print(repr(numunits(x)))
Output:
lt2a/forums/1 /home/forums 13> py numunits.py '144mHz' (144.0, 'mHz') '432 mHz' (432.0, 'mHz') '1.296GHz' (1.296, 'GHz') '2.304 GHz' (2.304, 'GHz') lt2a/forums/1 /home/forums 14>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split pdf in pypdf based upon file regex standenman 1 2,340 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  recall cool_person 1 1,122 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