string find/replace

Discussion in 'Python' started by Carlo, Dec 1, 2010.

  1. Carlo

    Carlo Guest

    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!
     
    Carlo, Dec 1, 2010
    #1
    1. Advertising

  2. Carlo

    Peter Otten Guest

    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'
     
    Peter Otten, Dec 1, 2010
    #2
    1. Advertising

  3. 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('_',' '))
     
    Donald O'Donnell, Dec 1, 2010
    #3
  4. Carlo

    Carlo Guest

    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!
     
    Carlo, Dec 1, 2010
    #4
  5. Carlo

    MRAB Guest

    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)
     
    MRAB, Dec 1, 2010
    #5
  6. Carlo <> 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 <>
    WWW: http://pietvanoostrum.com/
    PGP key: [8DAE142BE17999C4]
    Nu Fair Trade woonartikelen op http://www.zylja.com
     
    Piet van Oostrum, Dec 2, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    0
    Views:
    631
    Xah Lee
    Jun 14, 2006
  2. hokieghal99

    String find and replace

    hokieghal99, Aug 27, 2003, in forum: Python
    Replies:
    10
    Views:
    706
    hokiegal99
    Aug 27, 2003
  3. Alun
    Replies:
    3
    Views:
    4,575
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    245
    Dr John Stockton
    Aug 27, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    319
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page