Regular expression guaranteed to fail

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Tue Aug 24 11:14:06 EDT 2004


Greg Chapman wrote:
> Why not just "(?!)": this always fails immediately (since an empty pattern
> matches any string, the negation of an empty pattern match always fails).

It's fine for re.match.

'Why not?': Because I'd expect re.search to walk through the entire
string and check if each position in the string matches that regexp.
Unfortunately, a little timing shows that that happens with _every_
regexp suggested so far.  Long strings take longer for each of them.
(Except Jeremy's solution, of course, which avoids the whole problem.)
r'\A(?!)' or r'\Ax\A' didn't work either.

Anyway, I note that r'x\A' beats all the other regexps suggested so far
with a factor of 20 when searching 's'*10000.

-- 
Hallvard



More information about the Python-list mailing list