- It's not 100 % clear what exceptions you expect, but I presume you are trying to handle the case when the regex does not match. I suggest handling it this way: #Transformation #1 match = re.match(pattern, inword) result = match.group() if match else None - "after processing a few files, the script slows down" Have you considered the possibility that a particular file, or even a particular line is slow to process? A possible explanation to that would be that regexes can suffer from [*catastrophic backtracking*][1]. [1]: http://www.regular-expressions.info/catastrophic.html