Multiple replacements (was: The fundamentals...)

Simon Brunning SBrunning at trisystems.co.uk
Fri Jan 26 12:30:51 EST 2001


> From:	mspiggie at my-deja.com [SMTP:mspiggie at my-deja.com]
> I have been trying to find a way to use replace() to perform more than
> one replacement within a given file.  For instance, suppose I needed to
> replace ". " with "\n", but also need to replace "!" with "exclamation"
> and "?" with "question".  I have tried several approaches to this, but
> none of my neophyte ideas have produced satisfying results.
 
Some time ago, /F gave me this:

class MultiReplace:
    def __init__(self, replacements, wholeWords=None, caseInsensitive=None):
        self.wholeWords = wholeWords
        self.charMap = None
        self.replacements = replacements
        
        # Assume char to char mapping...
        if not wholeWords:
            charMap = map(chr, range(256))
            for fromVal, toVal in replacements.items():
                if len(fromVal) <> 1 or len(toVal) <> 1:
                    break
                if caseInsensitive:
                    charMap[ord(fromVal.upper())] = toVal
                    charMap[ord(fromVal.lower())] = toVal
                else:
                    charMap[ord(fromVal)] = toVal
            else:
                self.charMap = "".join(charMap)
                return

        # String to string mapping - use a regular expression        
        fromVals = replacements.keys()
        fromVals.sort() # lexical order

        if not wholeWords: # Build re pattern
            rePattern = '|'.join(map(re.escape, fromVals))
        else:
            rePattern = r'\b(' + '|'.join(map(re.escape, fromVals)) + r')\b'
        
        if caseInsensitive: # Compile re
            self.rePattern = re.compile(rePattern, re.I)
        else:
            self.rePattern = re.compile(rePattern)

    def replace(self, string):
        # apply replacement to string
        
        if self.charMap: # Char to char mapping
            return string.translate(self.charMap)

        # String to string mapping        
        return self.rePattern.sub(self.__replaceInMatch, string)
    
    def __replaceInMatch(self, match):
        item = match.group(0)
        return self.replacements.get(item, item)

I have made some changes to it since /F gave it to me, so if it's crap my,
it's my fault. ;-)

Use it like this:

>>>import MultiReplace
>>>replaceDict = {'a': 'b', 'c': 'd'} # From and to values in this
dictionary - strings can be longer than 1 character if you want.
>>>multiReplace = MultiReplace.MultiReplace(replaceDict)
>>>replace('spam')
'spbm'

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk
Are you going to come quietly, or do I have to use earplugs? - from The Goon
Show




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list