Title: Regex object should have introspection methods
Type: enhancement Stage:
Components: Regular Expressions Versions: Python 3.4
Status: pending Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, ezio.melotti, mattchaput, mrabarnett
Priority: normal Keywords:

Created on 2011-08-31 17:29 by mattchaput, last changed 2017-05-06 14:52 by serhiy.storchaka.

Messages (7)
msg143266 - (view) Author: Matt Chaput (mattchaput) Date: 2011-08-31 17:29
Several times in the recent past I've wished for the following methods on the regular expression object. These would allow me to speed up search and parsing code, by limiting the number of regex matches I need to try.

literal_prefix(): Returns any literal string at the start of the pattern (before any "special" parts). E.g., for the pattern "ab(c|d)ef" the method would return "ab". For the pattern "abc|def" the method would return "". When matching a regex against keys in a btree, this would let me limit the search to just the range of keys with the prefix.

first_chars(): Returns a string/list/set/whatever of the possible first characters that could appear at the start of a matching string. E.g. for the pattern "ab(c|d)ef" the method would return "a". For the pattern "[a-d]ef" the method would return "abcd". When parsing a string with regexes, this would let me only have to test the regexes that could match at the current character.

As long as you're making a new regex package, I thought I'd put in a request for these :)
msg143268 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-08-31 17:54
These additions sounds more useful as an external tool than regex functions/methods.  There are already a few tools able to "explain" what a regex matches.
The use cases you proposed are too specific to deserve new methods, and using them programmatically sounds like premature optimization IMHO.
msg143662 - (view) Author: Matt Chaput (mattchaput) Date: 2011-09-07 05:03
Ezio, no offense, but I think it's safe to say you've completely misunderstood this bug. It is not about "explaining what a regex matches" or optimizing the regex. Read the last sentences of the two paragraphs explaining the proposed methods for the use cases. This is about allowing MY CODE to programmatically get certain information about a regex object to allow it to limit the number of times it has to call regex.match(). AFAIK there's no good way to get this information about a regex object without adding these methods or building my own pure-Python regex interpreter, which would be both Herculean and pointless.
msg143681 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-09-07 14:34
Limiting the number of calls to re.match sounds like an optimization to me, and I still think that the methods you proposed are too specific.
msg143686 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2011-09-07 14:42
I tend to agree with Ezio.  Matt, maybe you could ask for other opinions on python-ideas?
msg143689 - (view) Author: Matt Chaput (mattchaput) Date: 2011-09-07 15:22
Yes, it's an optimization of my code, not the regex, as I said. Believe me, it's not premature. I've listed two general use cases for the two methods. To me it seems obvious that having to test a large number of regexes against a string, and having to test a single regex against a large number of strings, are two very common programming tasks, and they could both be speeded up quite a bit using these methods.

As of now my parsing code and other code such as PyParsing are resorting to hacks like requiring the user to manually specify the possible first chars of a regex at configuration. With the hacks, the code can be hundreds of times faster. But the hacks are error-prone and should be unnecessary. 

The PCRE library implements at least the "first char" functionality, and a lot more regex introspection that would be useful, through its pcre_fullinfo() function.
msg143696 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-09-07 16:32
If there is a generic introspection method like the pcre_fullinfo you mentioned, and if it's also useful and used with other languages/libraries, then it might be considered.
Date User Action Args
2017-05-06 14:52:30serhiy.storchakasetstatus: open -> pending
2012-11-18 22:02:31ezio.melottisetversions: + Python 3.4, - Python 3.3
2011-09-07 16:32:34ezio.melottisetmessages: + msg143696
2011-09-07 15:22:29mattchaputsetmessages: + msg143689
2011-09-07 14:42:38eric.araujosetnosy: + eric.araujo
messages: + msg143686
2011-09-07 14:34:21ezio.melottisetmessages: + msg143681
2011-09-07 05:03:03mattchaputsetmessages: + msg143662
2011-08-31 17:54:31ezio.melottisetnosy: + ezio.melotti, mrabarnett
messages: + msg143268
2011-08-31 17:29:33mattchaputcreate