Alex Rivera | Logout

Python regular expressions with more than 100 groups?

Asked 2009-01-25T22:48:56.240
22

Is there any way to beat the 100-group limit for regular expressions in Python? Also, could someone explain why there is a limit.

Edit
Report

2 Answers

6

If I'm not mistaken, the "new" regex module (currently third-party, but intended to eventually replace the re module in the stdlib) does not have this limit, so you might give that a try.

answered 2012-05-29T11:53:10.470
5

I'm not sure what you're doing exactly, but try using a single group, with a lot of OR clauses inside... so (this)|(that) becomes (this|that). You can do clever things with the results by passing a function that does something with the particular word that is matched:

 newContents, num = cregex.subn(lambda m: replacements[m.string[m.start():m.end()]], contents)

If you really need so many groups, you'll probably have to do it in stages... one pass for a dozen big groups, then another pass inside each of those groups for all the details you want.

answered 2009-01-26T21:58:27.057

Your Answer