Deleting specific characters from a string

Paul Rudin paul_rudin at scientia.com
Thu Jul 10 09:35:36 EDT 2003


>>>>> "Paul" == Paul Rudin <paul_rudin at scientia.com> writes:


    > r= reduce(lambda x,y: x+'|'+y, c,'')[1:]

It occurs to me that this isn't what you want if c contains special
regexp chararacters so really it should be:

r= reduce(lambda x,y: x+'|'+y, map(re.escape,c),'')[1:]

    > So putting it all together as an alternative version of your
    > fuction:


    > !!warning - untested code!!

    > import re

    > def stringReplace(s,c): 
    >    r= reduce(lambda x,y: x+'|'+y, c,'')[1:]
        
         r= reduce(lambda x,y: x+'|'+y, map(re.escape,c),'')[1:]


    >    return re.compile(r).sub('',s)




More information about the Python-list mailing list