In [22]:
import re
def dashrepl(matchobj):
    if matchobj.group(0) == '-':
        return '3'
    else: 
        return '4'

re.sub(r'-{1,2}', dashrepl, 'pro---gram-files')

#re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-22-4adccb91e15f> in <module>()
      8         return '4'
      9 
---> 10 re.sub(r'-{1,2}', dashrepl, 'pro---gram-files')
     11 
     12 #re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)

/home/leudis/anaconda2/lib/python2.7/re.pyc in sub(pattern, repl, string, count, flags)
    153     a callable, it's passed the match object and must return
    154     a replacement string to be used."""
--> 155     return _compile(pattern, flags).sub(repl, string, count)
    156 
    157 def subn(pattern, repl, string, count=0, flags=0):

<ipython-input-22-4adccb91e15f> in dashrepl(matchobj)
      3     if matchobj.group(0) == '-':
      4         return '3'
----> 5     elif matchobj.group(1) == '-':
      6         return '3'
      7     else:

IndexError: no such group

In [ ]:


In [ ]:


In [ ]: