[Python-ideas] Complicate str methods

Chris Angelico rosuav at gmail.com
Sat Feb 3 18:54:53 EST 2018


On Sun, Feb 4, 2018 at 10:43 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 2/3/2018 5:04 PM, Franklin? Lee wrote:
>> s.startswith, s.endswith:
>>      Allow argument to be a collection of strings.
>
>
> bool(re.match('|'.join(strings)) does exactly the proposed s.startswith,
> with the advantage that the actual match is available, and I think that one
> would nearly always want to know that match.
>
>>>> re.match('a|e|i|o|u', 'Franklin? Lee')
>>>> re.match('f|F', 'Franklin? Lee')
> <re.Match object; span=(0, 1), match='F'>

Picking up this one as an example, but this applies to all of them:
the transformation you're giving here is dangerously flawed. If there
are any regex special characters in the strings, this will either bomb
with an exception, or silently do the wrong thing. The correct way to
do it is (at least, I think it is):

re.match("|".join(map(re.escape, strings)), testme)

With that gotcha lurking in the wings, I think this should not be
cavalierly dismissed with "just 'import re' and be done with it".
Maybe put some recipes in the docs showing how to do this safely?

ChrisA


More information about the Python-ideas mailing list