string find/replace

Peter Otten __peter__ at web.de
Wed Dec 1 12:42:43 EST 2010


Carlo wrote:

> I want the Python equivalent of the Perl expression:
> s/([a-z])([A-Z])/\1 \2/g
> In plain language: place a space between a lowercase and uppercase
> letter. I get lost in the RE module. Can someone help me?

>>> import re
>>> re.compile("([a-z])([A-Z])").sub(r"\1 \2", "camelCase")
'camel Case'

or

>>> re.sub("([a-z])([A-Z])", r"\1 \2", "camelCase")
'camel Case'




More information about the Python-list mailing list