Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expressions with more than 100 groups?

Tags:

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

like image 351
Evan Fosmark Avatar asked Jan 25 '09 22:01

Evan Fosmark


2 Answers

There is a limit because it would take too much memory to store the complete state machine efficiently. I'd say that if you have more than 100 groups in your re, something is wrong either in the re itself or in the way you are using them. Maybe you need to split the input and work on smaller chunks or something.

like image 159
Keltia Avatar answered Oct 12 '22 07:10

Keltia


I found the easiest way was to

import regex as re 

instead of

import re 

The default _MAXCACHE for regex is 500 instead of 100 I believe. This is one of the many reasons I find regex to be a better module than re.

like image 26
zanbri Avatar answered Oct 12 '22 08:10

zanbri