title: python regex error: unbalanced parenthesis
body: <p>I pretty new to python, so i have a dictionary with some keys in it, and a string. I have to replace the string if a pattern found in the dictionary exists in the string. both the dictionary and string are very large. I'm using a regex to find the patterns.</p>
  
  <p>It all works fine until a key like this pops up '-(' or this '(-)' in which case python gives an error for unbalanced parenthesis. </p>
  
  <p>Here's how the code I've written looks: </p>
  
  <pre><code>somedict={'-(':'value1','(-)':'value2'}
  somedata='this is some data containing -( and (-)'
  for key in somedict.iterkeys():
      somedata=re.sub(key, 'newvalue', somedata)
  </code></pre>
  
  <p>Here's the error I've got in the console</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;console&gt;", line 2, in &lt;module&gt;
    File "C:\Python27\lib\re.py", line 151, in sub
      return _compile(pattern, flags).sub(repl, string, count)
    File "C:\Python27\lib\re.py", line 244, in _compile
      raise error, v # invalid expression
  error: unbalanced parenthesis
  </code></pre>
  
  <p>I've also tried it many ways using the regex compiler and searched a lot but didn't find anything addressing the problem. Any help is appreciated.</p>