Deleting specific characters from a string

Paul Rudin paul_rudin at scientia.com
Thu Jul 10 09:28:42 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: re.escape(x)+'|'+re.escape(y), 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: re.escape(x)+'|'+re.escape(y), c,'')[1:]    

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




More information about the Python-list mailing list