[Python-ideas] Proposed convenience functions for re module
BJörn Lindqvist
bjourne at gmail.com
Wed Jul 22 20:29:31 CEST 2009
2009/7/22 Steven D'Aprano <steve at pearwood.info>:
> The rationale is to make the following idiom easier:
>
>
> m = re.match(s, pattern1)
> if not m:
> m = re.match(s, pattern2)
> if not m:
> m = re.match(s, pattern3)
> if not m:
> m = re.match(s, pattern4)
> if m:
> m.group()
>
>
> which will become:
>
> m = re.multimatch(s, pattern1, pattern2, pattern3, pattern4)
> if m:
> m.group()
>
>
> Is there any support or objections to this proposal? Any comments?
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. 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
--
mvh Björn
More information about the Python-ideas
mailing list