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

Re: BUG: ${var##(|A)} fails to find longest prefix



But notice that here (A|) is followed by an extra A in the pattern: s=AA; echo ${s#(A|)A}

Since the first "A" of s=AA matches the "A" in "(A|)", the matcher should proceed and try to match the second "A" of s=AA to the second "A" of the pattern. Since that succeeds too, the expansion should return the empty string. But here somehow the pattern matcher backtracks to find out that s=AA also matches the second alternative of "(A|)" and that then the remaining "AA" still matches the second "A" of the pattern, which leads to a shorter match and to the printing of "A".

Philippe



On Tue, May 13, 2025 at 12:23 AM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
On Mon, May 12, 2025 at 3:57 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>>
>> There's no backtracking for X|Y
>
> Really? But then why doesn't  s=AA; echo ${s#(A|)A} print the empty string?

It doesn't matter whether you're looking for longest or shortest, X|Y
is always left to right.  In the above (A|) will always match A, and
(|A) will always match the empty position before or between two
substrings.  It'll only try the right branch if the left branch
doesn't match or if backtracking of the entire pattern rewinds to
before the open paren.


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