开发者

Replacing leading text in Python

I use Python 2.6 and I want to replace each instance of certain leading characters (., _ and $ in my case) in a string with another character or string. Since in my case the replacement string is the same, I came up with this:

def replaceLeadingCharacters(string, old, new = ''):
    t = string.lstrip(old)

    return new * (len(string) - len(t)) + t

which seems to work fine:

>>> replaceLeadingCharacters('._.!$XXX$._', '._$', 'Y')
'YYY!$XXX$._'
  • Is there a better (simpler or more efficient) way to achieve the same effect in Python ?

  • Is there a way to achieve this effect with a string instead of characters? Something like str.replace() that stops once something different than the string-to-be-replaced comes up in the input string? Right now I've come up with this:

    def replaceLeadingString(string, old, new = ''):
        n = 0
        o = 0
        s = len(old)
    
        while string.startswith(old, o):
            n += 1
            o += s
    
        return new * n + string[o:]
    

    I am hoping that there is a way to do this without an explicit loop

EDIT:

There are quite a few answers using the re module. I have a couple of questions/issues with it:

  • Isn't it significantly slower than the str methods when used as a replacement for them?

  • Is there an easy way to properly quote/escape strings that will be used in a regular expression? For example if I开发者_JAVA百科 wanted to use re for replaceLeadingCharacters, how would I ensure that the contents of the old variable will not mess things up in ^[old]+ ? I'd rather have a "black-box" function that does not require its users to pay attention to the list of characters that they provide.


Your replaceLeadingCharacters() seems fine as is.

Here's replaceLeadingString() implementation that uses re module (without the while loop):

#!/usr/bin/env python
import re

def lreplace(s, old, new):
    """Return a copy of string `s` with leading occurrences of
    substring `old` replaced by `new`.

    >>> lreplace('abcabcdefabc', 'abc', 'X')
    'XXdefabc'
    >>> lreplace('_abc', 'abc', 'X')
    '_abc'
    """
    return re.sub(r'^(?:%s)+' % re.escape(old),
                  lambda m: new * (m.end() / len(old)),
                  s)

Isn't it significantly slower than the str methods when used as a replacement for them?

Don't guess. Measure it for expected input.

Is there an easy way to properly quote/escape strings that will be used in a regular expression?

re.escape()


re.sub(r'^[._$]+', lambda m: 'Y' * m.end(0), '._.!$XXX$._')

But IMHO your first solution is good enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜