[Tutor] reading parts of a input string into different variables based on units.
kent37 at tds.net
Thu Mar 20 11:45:37 CET 2008
Kent Johnson wrote:
> One regex can split apart a numeric part and a non-numeric unit:
A little explanation:
> In : import re
> In : splitter = re.compile(r'(\d+)(\S+)')
The regex finds one or more digits \d+ followed by one or more
non-whitespace characters \S+. The parentheses define groups which will
be returned by the match.
> In : splitter.findall('2m 4cm 3mm')
> Out: [('2', 'm'), ('4', 'cm'), ('3', 'mm')]
findall() finds all matches for the regex in the string. When the regex
contains groups, the value returned is a list of tuples. Each tuple
contains the value of all the groups in the regex.
> If you want to allow decimals change the regex to r'([\d.]+)(\S+)'
This makes the first group match digits and period instead of just digits.
More information about the Tutor