[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Serhiy Storchaka
On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez > wrote: The OP thinks that the case for wanting just the string for a first regex match, or a verifiable default if there is no match, is way too common, that the advice on the web is not very good (it shoul

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Oscar Benjamin
> On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez wrote: is troublesome. >> >> The proposed implementation of a findfirst() would handle many common cases, >> and be friendly to newcomers (why do I need to deal with a Match object?), >> specially if the semantics are those of findall(): >> >> n

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Guido van Rossum
Well said! On Wed, Dec 4, 2019 at 16:33 Juancarlo Añez wrote: > > > On Wed, Dec 4, 2019 at 3:02 PM Guido van Rossum wrote: > >> Fair enough. I’ll let the OP defend his use case. >> > > The OP thinks that the case for wanting just the string for a first regex > match, or a verifiable default if

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Guido van Rossum
Fair enough. I’ll let the OP defend his use case. On Wed, Dec 4, 2019 at 10:51 Serhiy Storchaka wrote: > 04.12.19 20:01, Guido van Rossum пише: > > On Wed, Dec 4, 2019 at 9:18 AM Serhiy Storchaka > > > > wrote: > > > > [Guido] > > > I think I am +1 on > >

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Serhiy Storchaka
04.12.19 20:01, Guido van Rossum пише: On Wed, Dec 4, 2019 at 9:18 AM Serhiy Storchaka > wrote: [Guido]  > I think I am +1 on  > adding re.findfirst() as proposed by the OP. It is not clear what it should return. A Match object, a string, a tup

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Guido van Rossum
On Wed, Dec 4, 2019 at 9:18 AM Serhiy Storchaka wrote: > [Guido] > > I think I am +1 on > > adding re.findfirst() as proposed by the OP. > > It is not clear what it should return. A Match object, a string, a > tuple, whatever? What should it return if no match found -- None, en > empty string,

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Serhiy Storchaka
04.12.19 18:05, Guido van Rossum пише: On Wed, Dec 4, 2019 at 12:34 AM Serhiy Storchaka > wrote: `next(re.finditer(...), None)` is a weird way of writing `re.search(...)`. `next(re.finditer(...), defaults)` is the same as `re.search(...) or defaults`

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Guido van Rossum
On Wed, Dec 4, 2019 at 8:54 AM Random832 wrote: > On Wed, Dec 4, 2019, at 11:05, Guido van Rossum wrote: > > Not so fast. re.search() returns a Match object, while re.finditer() > > and re.findall() return strings. For people who are just interested in > > strings, the Match object is just a dist

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Random832
On Wed, Dec 4, 2019, at 11:05, Guido van Rossum wrote: > Not so fast. re.search() returns a Match object, while re.finditer() > and re.findall() return strings. For people who are just interested in > strings, the Match object is just a distraction. I think I am +1 on > adding re.findfirst() as

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Guido van Rossum
On Wed, Dec 4, 2019 at 12:34 AM Serhiy Storchaka wrote: > `next(re.finditer(...), None)` is a weird way of writing `re.search(...)`. > > `next(re.finditer(...), defaults)` is the same as `re.search(...) or > defaults`. > Not so fast. re.search() returns a Match object, while re.finditer() and re

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-04 Thread Serhiy Storchaka
04.12.19 03:46, Oscar Benjamin пише: result = next(re.finditer(...), None) if result is None: # raise or something else: # use result `next(re.finditer(...), None)` is a weird way of writing `re.search(...)`. `next(re.finditer(...), defaults)` is the same as `re.search(...) or defau

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Andrew Barnert via Python-ideas
On Dec 3, 2019, at 17:47, Oscar Benjamin wrote: > > On Tue, 3 Dec 2019 at 12:48, Paul Moore wrote: >> >> My impression is that he was asking for a re.findfirst(...) function >> to give a more discoverable name to the next(re.finditer((...)) idiom. >> >> As a single example of defining a dedic

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Random832
On Tue, Dec 3, 2019, at 20:46, Oscar Benjamin wrote: > What exactly is the idiom here? > > Using bare next is not a good idea because it leaks StopIteration > which can have awkward side effects. So are you suggesting something > like > > result = next(re.finditer(...), None) > if result is None:

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Oscar Benjamin
On Tue, 3 Dec 2019 at 12:48, Paul Moore wrote: > > My impression is that he was asking for a re.findfirst(...) function > to give a more discoverable name to the next(re.finditer((...)) idiom. > > As a single example of defining a dedicated function to replace a > one-liner, I think it's marginal

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Andrew Barnert via Python-ideas
On Dec 3, 2019, at 09:05, Serhiy Storchaka wrote: > > Actually the semantic of the above code is different from the semantic of > `re.findall(regex, text)[0]`. findall() yields strings if the pattern > contains less than 2 capture groups and tuples if it more than 1 capture > groups. > > >>>

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Serhiy Storchaka
03.12.19 13:53, Juancarlo Añez пише: There are many ugly recipes about to handle the common use case that could be handled by: def findfirst(regex, text, default=None, flags=0):      return next(finditer(regex, text, flags=flags), default=default) Oh, this is the most strange use of

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Paul Moore
On Tue, 3 Dec 2019 at 12:07, Steven D'Aprano wrote: > > I'm sorry Juancarlo, it's not clear to me what *precisely* your proposal > is. Are you asking for "findfirst" to be a builtin? A regex helper > function? A method on regex objects? Something else? > My impression is that he was asking for a

[Python-ideas] Re: Fwd: re.findfirst()

2019-12-03 Thread Steven D'Aprano
I'm sorry Juancarlo, it's not clear to me what *precisely* your proposal is. Are you asking for "findfirst" to be a builtin? A regex helper function? A method on regex objects? Something else? -- Steven ___ Python-ideas mailing list -- python-ideas@p