msmazh msmazh - 21 days ago 5
Python Question

Python regex raises the "nothing to repeat" error

I found couple of other posts talking about this issue, but still couldn't find out how to fix mine. Here is my code:

words = [':\)','sleeping','... :\(','facebook','tired','out of the',"i'm"]
regex = re.compile(r'\b%s\b' % '\\b|\\b'.join(words), flags=re.IGNORECASE)


I get this error:

error: nothing to repeat

Answer

There are two possibilities. Either you are running into a Python bug related to your regular expression, or you have an unescaped set of () inside your list. Either way, making sure that you're not accidentally matching regex pattern would probably be wise.

Have you tried this instead?

escaped = map(re.escape, words)
re.compile(r'\b%s\b' % '\\b|\\b'.join(escaped), flags=re.IGNORECASE)

All of that said, if your list really does have 1,600 elements, then perhaps regular expression isn't the best task for the job.