I want to be able to replace the spaces between the city and the number with a comma, but the line I came up with (and variations of it) seem to obliterate the string.
>>> temp = re.sub(r"(\w+).*?(\d+)", ",", string)
where string is like:
Toronto 239495
Cape Town 34567
How can I do this?
I’m still picking up regex, so any explanations with any code would be really great.
You’re replacing the right matches, but only with a comma! The other parts of the match get replaced too. You could use assertions, or just stick them back in:
However, the
\w+will only match theCapeinCape Town. How about: