Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: backreferences



On Fri, Oct 16, 2015 at 4:30 AM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Oct 15,  6:16pm, Ray Andrews wrote:
> } Subject: Re: backreferences
> }
> }     if [[ "$sstring" = (#b)([(^(edcba))]*)(edcba)(*) ]];
>
> Umm, no. [(^(edcba))] is still a character class (open paren, caret,
> e,d,c,b,a, close paren).  Just (^(edcba)) without the square brackets.
> And you have more parens then, so your $match[] indexes are wrong.
>
>         if [[ "$sstring" = (#b)((^(edcba))*)(edcba)(*) ]]
>
> There are 5 sets of parens, and you care about $match[1], $match[4],
> and $match[5].  $match[2] is the prefix of $match[1] that was not
> consumed by the middle *, and $match[3] is an empty substring of
> $match[2] (because it was excluded from matching).  Count off the
> open parens left to right to see this.
>
> In fact you don't even need the middle * because (^edcba) will eat
> an arbitrarily long string as long as it is not literally "edcba".
> So you can reduce this to
>
>         if [[ "$sstring" = (#b)(^edcba)(edcba)(*) ]]
>
> and then you're back to only needing $match[1,3].

As a sidenote, (^foo)* is always useless to write, since (^foo) will
expand to the empty string, and then the * will consume anything else.
A useful way to think of (^foo) is a * that will exclude any matches
that don't match the pattern foo.

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author