Monday, 15 February 2010

python - Replace space between a letter and a number with a comma? -



python - Replace space between a letter and a number with a comma? -

i want able replace spaces between city , number comma, line came (and variations of it) seem obliterate string.

>>> temp = re.sub(r"(\w+).*?(\d+)", ",", string)

where string like:

toronto 239495 cape town 34567

how can this?

i'm still picking regex, explanations code great.

you're replacing right matches, comma! other parts of match replaced too. utilize assertions, or stick them in:

temp = re.sub(r"(\w+).*?(\d+)", r"\1,\2", string)

however, \w+ match cape in cape town. how about:

temp = re.sub(r"(.+?)\s*(\d+)", r"\1,\2", string)

python regex

No comments:

Post a Comment