[Python-ideas] Proposed convenience functions for re module
MRAB
python at mrabarnett.plus.com
Wed Jul 22 20:44:02 CEST 2009
BJörn Lindqvist wrote:
> 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
>
re.match and re.search will accept either a string or a compiled pattern
as the first argument. Never used it myself, but...
More information about the Python-ideas
mailing list