[Python-ideas] Re: A string function idea

2022-03-29 Thread Christopher Barker
On Tue, Mar 29, 2022 at 4:08 PM Steven D'Aprano wrote: > I have no strong opinion on whether this simple function should be built > into the string class, I do -- this is not sufficiently general to be a string method. > but I do have a strong opinion about re-writing > it into a slower, mor

[Python-ideas] Re: A string function idea

2022-03-29 Thread Chris Angelico
On Wed, 30 Mar 2022 at 10:08, Steven D'Aprano wrote: > Here's the version of grab I used: > > def grab(text, start, end): > a = text.index(start) > b = text.index(end, a+len(start)) > return text[a+len(start):b] > This is where Python would benefit from an sscanf-style parser. Instead

[Python-ideas] Re: A string function idea

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 11:00:41AM +0300, Serhiy Storchaka wrote: > 28.03.22 15:13, StrikerOmega пише: > >And I want to grab some kind of value from it. > > There is a powerful tool designed for solving such problems. Is is > called regular expressions. > > >sample.grab(start="fruit:", end="\n")

[Python-ideas] Re: A string function idea

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 12:35:56AM -0700, Paul Bryan wrote: > I wonder if applying regular expressions would sufficiently address > your use case. 'Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.' -- Jamie Zawinski Apart fro

[Python-ideas] Re: A string function idea

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 09:12:36AM +0200, StrikerOmega wrote: > The grab function would find the index of the first occurrence of the > "start" string in the parent string and then the next occurrence of the > "end" string starting from that index and return the substring between > those. That's

[Python-ideas] Re: A string function idea

2022-03-29 Thread Abdulla Al Kathiri
Thanks for teaching me about the __getitem__ method of the Match class. I always do .group(1) before. Abdulla Sent from my iPhone > On 29 Mar 2022, at 12:02 PM, Serhiy Storchaka wrote: > > re.search(r'fruit:(.*?)\n', sample)[1] ___ Python-ideas mai

[Python-ideas] Re: A string function idea

2022-03-29 Thread Serhiy Storchaka
28.03.22 15:13, StrikerOmega пише: And I want to grab some kind of value from it. There is a powerful tool designed for solving such problems. Is is called regular expressions. sample.grab(start="fruit:", end="\n") >> 'apple' re.search(r'fruit:(.*?)\n', sample)[1] sample.grab(start="tr

[Python-ideas] Re: A string function idea

2022-03-29 Thread Paul Bryan
I wonder if applying regular expressions would sufficiently address your use case. On Mon, 2022-03-28 at 14:13 +0200, StrikerOmega wrote: > Hello everyone, > > When I am coding in Python I often encounter situations where I have > a string like this one. > > sample=""" > fruit:apple > tree:[Appl

[Python-ideas] Re: A string function idea

2022-03-29 Thread Chris Angelico
On Tue, 29 Mar 2022 at 18:13, StrikerOmega wrote: > > The grab function would find the index of the first occurrence of the "start" > string in the parent string and then the next occurrence of the "end" string > starting from that index and return the substring between those. > This sounds lik

[Python-ideas] Re: A string function idea

2022-03-29 Thread StrikerOmega
The grab function would find the index of the first occurrence of the "start" string in the parent string and then the next occurrence of the "end" string starting from that index and return the substring between those. So in the example: sample = "sqrt(sin(x) + cos(y))" The grab function would

[Python-ideas] Re: A string function idea

2022-03-28 Thread Steven D'Aprano
On Mon, Mar 28, 2022 at 02:13:32PM +0200, StrikerOmega wrote: > You can also "grab" values enclosed in brackets or in any kind of character > and It works as you would expect. > > sample.grab(start="tree:[", end="]") > >> 'Apple tree' If we have sample = "sqrt(sin(x) + cos(y))" and sample.