Multiple, specific, regex substitutions in Python
Question: What I would like to do is to make specific substitions in a given text. For
example, '<' should be changed to '[', '>' to ']', and so forth. It is similar
to the solution given here: [How can I do multiple substitutions using regex
in python?](http://stackoverflow.com/questions/15175142/how-can-i-do-multiple-
substitutions-using-regex-in-python), which is

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

Now, the problem is that I would also like to replace regex-matched patterns.
For example, I want to replace 'fo.+' with 'foo' and 'ba[rz]*' with 'bar'.

Removing the map(re.escape in the code helps, so that the regex actually
matches, but I then receive key errors, because, for example, 'barzzzzzz'
would be a match, and something I want to replace, but 'barzzzzzz' isn't a key
in the dictionary, the literal string 'ba[rz]*' is. How can I modify this
function to work?

(On an unrelated note, where do these 'foo' and 'bar' things come from?)


Answer: Just do multiple `sub` calls.

On an unrelated note, Jargon File to the rescue: [Metasyntactic
variables](http://www.catb.org/jargon/html/M/metasyntactic-variable.html),
[foo](http://www.catb.org/jargon/html/F/foo.html).

