Multiple, specific, regex substitutions in Python -


what make specific substitions in given text. example, '<' should changed '[', '>' ']', , forth. similar solution given here: how can multiple substitutions using regex in python?, is

import re   def multiple_replace(dict, text):   # create regular expression  dictionary keys   regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))    # each match, look-up corresponding value in dictionary   return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)  

now, problem replace regex-matched patterns. example, want replace 'fo.+' 'foo' , 'ba[rz]*' 'bar'.

removing map(re.escape in code helps, regex matches, receive key errors, because, example, 'barzzzzzz' match, , want replace, 'barzzzzzz' isn't key in dictionary, literal string 'ba[rz]*' is. how can modify function work?

(on unrelated note, these 'foo' , 'bar' things come from?)

import re  def multiple_replace(dict, text):   # create regular expression  dictionary keys   regex = re.compile(r'(%s)' % "|".join(dict.keys()))   return regex.sub(lambda mo: dict[       [ k k in dict if       re.search(k, mo.string[mo.start():mo.end()])       ][0]], text)  d = { r'ba[rz]*' : 'bar', '<' : '[' } s = 'barzzzzzz <'  print multiple_replace(d, s) 

gives:

bar [ 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -