string find/replace

Donald O'Donnell donnieodonnell at gmail.com
Wed Dec 1 12:46:03 EST 2010


On Dec 1, 12:36 pm, Carlo <ca... at somewhere.com> wrote:
> Hello,
>
> 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?
>
> Thanks!

This will also replace '_' with space::


recamelsub = re.compile(r"([a-z])([A-Z])").sub

def spacify(string):
    """
    Replace '_' with space & insert space between lower & upper case
letters
    """
    return recamelsub(r"\1 \2", string.replace('_',' '))



More information about the Python-list mailing list