Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > string find/replace

Reply
Thread Tools

string find/replace

 
 
Carlo
Guest
Posts: n/a
 
      12-01-2010
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!
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-01-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'

 
Reply With Quote
 
 
 
 
Donald O'Donnell
Guest
Posts: n/a
 
      12-01-2010
On Dec 1, 12:36*pm, Carlo <(E-Mail Removed)> 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('_',' '))
 
Reply With Quote
 
Carlo
Guest
Posts: n/a
 
      12-01-2010
On 2010-12-01, Peter Otten <(E-Mail Removed)> wrote:
>>>> import re
>>>> re.compile("([a-z])([A-Z])").sub(r"\1 \2", "camelCase")

> 'camel Case'


Very simple if you know it. Thank you!
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      12-01-2010
On 01/12/2010 17:36, Carlo 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?
>

That's easy:

new_text = re.sub(r'([a-z])([A-Z])', r'\1 \2', old_text)
 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      12-02-2010
Carlo <(E-Mail Removed)> writes:

> On 2010-12-01, Peter Otten <(E-Mail Removed)> wrote:
>>>>> import re
>>>>> re.compile("([a-z])([A-Z])").sub(r"\1 \2", "camelCase")

>> 'camel Case'

>
> Very simple if you know it. Thank you!


And almost as cryptic as Perl!!
--
Piet van Oostrum <(E-Mail Removed)>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Nu Fair Trade woonartikelen op http://www.zylja.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments