![](https://secure.gravatar.com/avatar/5615a372d9866f203a22b2c437527bbb.jpg?s=120&d=mm&r=g)
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