I have a set of strings in which numbers can be separated by different characters, or letters:
12
14:45:09
2;32
04:43
434.34
43M 343ho
I want to get a list of these numbers for each row:
[12]
[14, 45, 9]
[2, 32]
[4, 43]
[434, 34]
[43, 343]
I try to do so, but this does not work:
>>> import re
>>> pattern = r'(\d*)'
>>> re.split(pattern, '12')
['', '12', '', '', '']
>>> re.split(pattern, '14:45:09')
['', '14', '', '', ':', '45', '', '', ':', '09', '', '', '']
>>> pattern = r'([0-9]*)'
>>> re.split(pattern, '14:45:09')
['', '14', '', '', ':', '45', '', '', ':', '09', '', '', '']
>>> re.split(pattern, '43M 343ho')
['', '43', '', '', 'M', '', ' ', '343', '', '', 'h', '', 'o', '', '']
>>>
How can this be done correctly?
re.split(r'\D+',s)re.splityou can usere.findall(r'\d+', line)343hogive 34?