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

Search Discussions

  • Peter Otten at Dec 1, 2010 at 5:42 pm

    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'
  • Carlo at Dec 1, 2010 at 5:59 pm

    On 2010-12-01, Peter Otten wrote:
    import re
    re.compile("([a-z])([A-Z])").sub(r"\1 \2", "camelCase")
    'camel Case'
    Very simple if you know it. Thank you!
  • Piet van Oostrum at Dec 2, 2010 at 4:17 pm

    Carlo <carlo at somewhere.com> writes:
    On 2010-12-01, Peter Otten 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 <piet at vanoostrum.org>
    WWW: http://pietvanoostrum.com/
    PGP key: [8DAE142BE17999C4]
    Nu Fair Trade woonartikelen op http://www.zylja.com
  • Donald O'Donnell at Dec 1, 2010 at 5:46 pm

    On Dec 1, 12:36?pm, 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?

    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('_',' '))
  • MRAB at Dec 1, 2010 at 6:07 pm

    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)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 1, '10 at 5:36p
activeDec 2, '10 at 4:17p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase