title: Escaping special characters like ) in regular expressions in Python
body: <p>I have a dictionary and string like :</p>
  
  <pre><code>d = {'ASAP':'as soon as possible', 'AFAIK': 'as far as I know'}
  s = 'I will do this ASAP, AFAIK.  Regards, X'
  </code></pre>
  
  <p>I want to replace the values of dict with the keys of the dict in the string and return</p>
  
  <pre><code>I will do this &lt;as soon as possible&gt;, &lt;as far as I know&gt;.  Regards, X.
  </code></pre>
  
  <p>I use </p>
  
  <pre><code>pattern = re.compile(r'\b(' + '|'.join(d.keys())+r')\b')
  result=pattern.sub(lambda x: '&lt;'+d[x.group()]+'&gt;',s)
  print"result:%s" % result
  </code></pre>
  
  <p>I have a dictionary like:</p>
  
  <pre><code>{'will you wash some pants for me please :-)': 'text'}
  </code></pre>
  
  <p>The smiley is causing an error.  How do I change my regular expression to suit any characters like smileys?</p>