Python regular expression implementation details

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2009-05-09T22:12:50Z Indexed on 2010/03/26 0:33 UTC
Read the original article Hit count: 496

Filed under:
|
|

A question that I answered got me wondering:

How are regular expressions implemented in Python? What sort of efficiency guarantees are there? Is the implementation "standard", or is it subject to change?

I thought that regular expressions would be implemented as DFAs, and therefore were very efficient (requiring at most one scan of the input string). Laurence Gonsalves raised an interesting point that not all Python regular expressions are regular. (His example is r"(a+)b\1", which matches some number of a's, a b, and then the same number of a's as before). This clearly cannot be implemented with a DFA.

So, to reiterate: what are the implementation details and guarantees of Python regular expressions?

It would also be nice if someone could give some sort of explanation (in light of the implementation) as to why the regular expressions "cat|catdog" and "catdog|cat" lead to different search results in the string "catdog", as mentioned in the question that I referenced before.

© Stack Overflow or respective owner

Related posts about python

Related posts about regex