On 2019-12-06 21:16, Random832 wrote:
On Fri, Dec 6, 2019, at 14:50, MRAB wrote:
On 2019-12-06 18:24, Andrew Barnert via Python-ideas wrote:
> On Dec 6, 2019, at 09:51, Random832 <[email protected]> wrote:
>>
>> If match objects are too hard to use, maybe they should be made more user-friendly? What about adding str and iterable semantics to match objects so it can be used as str(re.search(...)); tuple(re.search(...)); a, b = re.search(...)?
>
> That’s a clever idea, and it might work.
>
1. Match objects are also be returned by re.match, and you wouldn't
expect that to look for more matches.
I'm not sure what you meant by looking for more matches, though I suspect it's
because, as below, I wasn't clear with what I meant by iterable semantics.
2. What would tuple(re.search(...)) do? Wouldn't it do the same as
tuple(re.findall(...))?
I intended the tuple [well, the iterable semantics that would allow the tuple
call to succeed] to return m.groups(), i.e. the same tuple as re.findall()[0]
does when the re contains capturing groups. Sorry for not making that clear
enough.
3. a, b = re.search(...) would fail if it didn't return exactly 2
matches
2 capturing groups, not 2 matches. Again, sorry for not making that explicitly
clear.
Ah, OK.
Well, match objects already accept subscripting as an alternative to
.group(), so:
re.search(...)[0]
and in the regex module it also supports slices, so:
regex.search(...)[1 : ]
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/QFCMWPLBOUAJYBQFOOOYREC2E2J2DY3Z/
Code of Conduct: http://python.org/psf/codeofconduct/