Regexp optimization question

William Park opengeometry at yahoo.ca
Fri Apr 23 15:41:01 EDT 2004


Magnus Lie Hetland <mlh at furu.idi.ntnu.no> wrote:
> Now I can do something like this:
> 
>   hits = []
>   for pat in pats:
>       hits.extend(pat.finditer(text))
>   # Maybe sort them in order of occurrence
> 
> *Or* I can do something like this:
> 
>   bigPat = '(' + '|'.join(pats) + ')'
>   hits = list(bigPat.finditer(text))
> 
> The last one is *much* faster -- but only if I forego named groups.
> And without them, I can't find out which patterns matched at which
> locations. (I can, as suggested, use .lastindex to find *one* of them,
> but not all, as I need.)

Since you want both the matched strings and their locations in file, you
pretty much have to this manually, one by one.

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution/training/migration, Thin-client



More information about the Python-list mailing list