How would I search and replace multiple regexes using python re [duplicate]
Possible Duplicate:
Python replace multiple strings
I am looking to replace “ “
, “\r”
, “\n”
, “<”
, “>”
, “’”
(single quote), and ‘”’
(double quote) with “”
(empty). I’m also looking to replace “;”
and “|”
with “,”
.
Would this be handled by re.search
since I want to be able to search anywhere in the text, or would I use re.sub
.
What would be the best way to handle this? I have found bits a开发者_如何学编程nd pieces, but not where multiple regexes are handled.
If you need to replace only single characters then you could use str.translate()
:
import string
table = string.maketrans(';|', ',,')
deletechars = ' \r\n<>\'"'
print "ex'a;m|ple\n".translate(table, deletechars)
# -> exa,m,ple
If you want to remove all occurrences of those characters, just put them all in a character class and do re.sub()
your_str = re.sub(r'[ \r\n\'"]+', '', your_str)
your_str = re.sub(r'[;|]', ',', your_str)
You have to call re.sub()
for every replacement rule.
import re
reg = re.compile('([ \r\n\'"]+)|([;|]+)')
ss = 'bo ba\rbu\nbe\'bi"by-ja;ju|jo'
def repl(mat, di = {1:'',2:','}):
return di[mat.lastindex]
print reg.sub(repl,ss)
Note: '|' loses its speciality between brackets
精彩评论