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 return:

sample.grab(start="sqrt(", end=")")
>> "sin(x"

This shows that "grab" is only useful given that you specify the "start" and "end" delimiters unambiguously. It depends on that to produce the correct output.

Julio Cabria
Engineering student
Autonomous University of Madrid

On Tue, Mar 29, 2022, Steven D'Aprano <steve@pearwood.info> wrote:
> 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.grab(start="sqrt(", end=")") which of these does it return?
>
> 1. "sin(x"
>
> 2. "sin(x) + cos(y)"
>
>
> --
> Steve
>