[Python-ideas] Proposed convenience functions for re module

Steven D'Aprano steve at pearwood.info
Thu Jul 23 01:00:01 CEST 2009


On Thu, 23 Jul 2009 04:29:31 am BJörn Lindqvist wrote:

> I don't like it very much because it would only work for uncompiled
> patterns. All functions in re has a RegexObject counterpart, but
> multisearch() and multimatch() obviously would not. 

That's incorrect -- they accept pre-compiled regexes as well as strings.

>>> pat = re.compile('a.c')
>>> multimatch("abcd", ['z.*z', pat])
<_sre.SRE_Match object at 0xb7cd8090>

> For the quoted 
> example I'd usually try to create one regex that matches all four
> patterns, or use a loop:
>
> for pat in (pattern1, pattern2, pattern3, pattern4):
>     m = re.match(s, pat)
>     if m:
>         m.group()
>         break

Apart from being in a function, my proposal (which you claim to dislike) 
is virtually identical to that code (which you say you use).




-- 
Steven D'Aprano



More information about the Python-ideas mailing list