I have some string like this
' 12 2 89 29 11 92 92 10'
(all the numbers are positive integers so no -
and no .
), and I want to extract all numbers from it, edit some of the numbers, and then put them all together with the same whitespaces. For example, if I change the number 11
to 22
, I want the final string as
' 12 2 89 29 22 92 92 10'
I did some search and most questions disregard the whitespaces and only care about the numbers. I tried
match = re.match((\s*(\d+)){8}, str)
but match.group(0)
gives me the whole string,, match.group(1)
gives me the first match \ 12
(I added the \
otherwise the website won't show the leading whitespaces), and match.group(2)
gives me 12
. But it won't give me any numbers after that, any index higher than 2
gives me an error. I don't think my approach is the correct one, what is the right way to do this?
I just tried re.split('(\d+)', str)
and that seems to be what I need.