string find/replace

C

Carlo

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!
 
P

Peter Otten

D

Donald O'Donnell

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('_',' '))
 
M

MRAB

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?
That's easy:

new_text = re.sub(r'([a-z])([A-Z])', r'\1 \2', old_text)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top