May-18-2022, 06:29 PM
(This post was last modified: May-18-2022, 06:30 PM by Gribouillis.)
If you accept only the syntax of Python numbers, as in the previous post, you could use tokenize
By the way, in order to specify the problem clearly, it would be good to write a complete syntax of the strings that you want to be able to parse.
>>> import io >>> from tokenize import tokenize >>> def parse(s): ... t = tokenize(io.BytesIO(s.encode()).readline) ... next(t) ... x, u = next(t), next(t) ... return (x.string, u.string) ... >>> for a in ['144mHz','432 mHz','1.296GHz','2.304 GHz']: ... print(repr(a), parse(a)) ... '144mHz' ('144', 'mHz') '432 mHz' ('432', 'mHz') '1.296GHz' ('1.296', 'GHz') '2.304 GHz' ('2.304', 'GHz')But you cannot extend the syntax to allow fancy numbers representation.
By the way, in order to specify the problem clearly, it would be good to write a complete syntax of the strings that you want to be able to parse.