From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 25980 invoked from network); 14 Sep 2022 03:21:02 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 14 Sep 2022 03:21:02 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1663125662; b=g/EexjqqvQ7pncka3+Fg+jfZOIL7ZHAoRD7mc5qYJuSvQc2NxkM/Q2m9eS/FzS62XVui3LInzZ Szw0j5BQ4dx4JzULWFUeD1uwV3v5TD+4Y0iL3fB4M/kXEsMB1fLim8agRev4hxVBpEBA6XXEaO c4+I7onk4EOq3Cwc2rEu5Inb1PFtkzcjrGO1o3j52cjlAw+fRPmVup8lK3byyKvrOpn9bj9QQV WyTVDC0vOqq37hz4VxNx0L9MUcVcQU99GT6UEB+cW+gs6hxWTS/ZZm21QtzQNeHB+teDV8XXCo OuLDHmIUy/VdXNOsm1u9Qm2rlHRa8kgdv4CW8aOzfZ6IPQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-pj1-f41.google.com) smtp.remote-ip=209.85.216.41; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1663125662; bh=sj2qv6/FZ8KdP27KageS99nj4v6YIuii7Uon1NE0idg=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=KxpGIR9M8Bquu8DavnfM+6nd0rPWIaTp5l+iTju/5Ia+xTW8kUL80zFwPKylH76ra/iyQhEn5n BVBgGdUQN5gpqRMni6f5rFsDFDaqQLdnVJcFqSV+Kb0f2qznL/o8agPCpfumcdiED9Z0wCSYNL qF2PgFVzLZAXTRQfn8TmySAA/M4gumSHZ9Et4w5YeRtTkiq/szirn99Uo75ljEkCW+lHelgXYh LetpX92Vqj5csqw2LeqFxYaRfAQhnL1ySc2Rf+pQBmDBb6FhByENGcrs+C7Ksu+gGdGGpls4hN 3P2g2BxWLU2s7PGH+hVLyJMkQWzo5woZx6Nm6SOfPOSUUQ==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=P5cAYv7XlQIit3mJ3ozp4iJA3gYcd/axhrv49wusBho=; b=i161aAeWfxs2VebjIsdgPqL7b8 z836k7ZPCBnpktGGEyA3vLqlVs9lYL2NrsSEWVEgqIbFk8gcTRj0PtyP6DBYSQKdGxyI2atXft7Ae P3HBeMjvw+31TmR3OieC2vo4Y7OoqZcI7qGJsmoW3mkcE3nRycvwzqkjiHD/rCrkfP3ytcanfEwmC XRaNAPTsEHFqHMziZIJH/CXL//TNfJEWxv1EwQLX4cgZJ0wDEsptcO4qpZ9UntYvkvoovEEDI0+by jcPW9T/o8EUsU6a+djfM9ZVF7CZXQnupqjLn24rozW2FstqhXMIOo96K3c7mO8lYJ57E54iOm2Lib eRuZldGg==; Received: from authenticated user by zero.zsh.org with local id 1oYIxR-000OI5-Kz; Wed, 14 Sep 2022 03:21:01 +0000 Authentication-Results: zsh.org; iprev=pass (mail-pj1-f41.google.com) smtp.remote-ip=209.85.216.41; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-pj1-f41.google.com ([209.85.216.41]:43771) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1oYIwn-000Nae-9V; Wed, 14 Sep 2022 03:20:22 +0000 Received: by mail-pj1-f41.google.com with SMTP id o70-20020a17090a0a4c00b00202f898fa86so3728799pjo.2 for ; Tue, 13 Sep 2022 20:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=P5cAYv7XlQIit3mJ3ozp4iJA3gYcd/axhrv49wusBho=; b=YStdWpTDv8MfSLAjDyoSCV0IjGFyr2CIjG7zDmlx3JoRZGqtSjB0B0BUwTvOMydd2V OjmweuwcCtt90E27bVA66uHQx8zeRSMygBx/7dD6WbBenVtEBqvqC2J4DtMGi1zcnlMV kpiXKdaSANboY7qrwQcCImBQbPJM3LdolGFpiJV/fxruefnY9ut0hskZQDnPSZnnpnBA WPTPjqHDI/2F2SjWbG/JoUs5cz+R0rBj2g6IBFiwQDzhBGHI82FfWarN8GPRl20mMj1P cwG3QHlhGDe3pBgWEWIq3v7nQoVxocRx2DEm+SepYpwM8MdRLvIbBKVeKj5M6umYkogY XGKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=P5cAYv7XlQIit3mJ3ozp4iJA3gYcd/axhrv49wusBho=; b=zLLJZtu4a0xmMD28TmaKa1I446KQER9WnRi1hR84PFlD+3RefuH0XifO3+u43yXsBh MXiw177KyycxYsUjrxtuxeagqgnJu8IYo1SFqn+eaHKwyM59Qo/dbqQRGRos8L/Zmgol IPXf+I9du56iy0lDlQTqsxxZVtpFBBvq5mdygOtf8I5a9d7husNeXx1VF5ZFNpaMjXLd Kz40CZcA7IFgKINPbP8SFDhZ0tANuOcTOhH65hKbYo1rQnKjzaoJ0leRbl4sXnOR/0qv b+9BWj/WmyBwuAxtJbr5h50PDoh7391cQXKEUrXrhG1RFLKg6u96DXJnx1pHE5/9T0eW e40g== X-Gm-Message-State: ACrzQf0p88PzPHgZlih1LrtQfDCuDiLBDZ99rcTV0jnF0T98HdE4IMT7 Quc3VKeAHdpDVjAVLGXLlYPMhyt5JAiIo8BmYAYYHWmUdYLhqQ== X-Google-Smtp-Source: AMsMyM6yc0VHK7mHNh12yvNWim/LN5+qVOgDi+Mb2X1EiF77g0Fo9GIVMDDOyuCnLLykqtmgikFDt8D7P6qdwYW0We8= X-Received: by 2002:a17:90a:4607:b0:202:e22d:489c with SMTP id w7-20020a17090a460700b00202e22d489cmr2543673pjg.80.1663125619564; Tue, 13 Sep 2022 20:20:19 -0700 (PDT) MIME-Version: 1.0 References: <4844b850-2ca6-4c90-95ae-aae00dc339cf@www.fastmail.com> In-Reply-To: From: Michele Venturi Date: Wed, 14 Sep 2022 05:20:08 +0200 Message-ID: Subject: Re: Substitute the last match of a pattern during parameters expansion. To: Bart Schaefer Cc: zsh-users@zsh.org Content-Type: multipart/alternative; boundary="000000000000c5669105e89a9bf6" X-Seq: 28068 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: --000000000000c5669105e89a9bf6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Sep 14, 2022, 01:11 Bart Schaefer wrote= : > On Sun, Sep 11, 2022 at 11:01 PM Lawrence Vel=C3=A1zquez > wrote: > > > > % foo=3DaXbXcXdXe > > % print ${(*)foo/(#b)(*)X/$match[1]Y} > > That works for the last match, but not for the Nth-from-last. (It > also works only in zsh 5.9+, before that you need extendedglob set.) > > You can do it more generally like this: > > N=3D0 # Number of X to skip over when counting from the right > print -- ${(*)foo/%(#b)(*)X(*(X*)(#c$N))/$match[1]Y$match[2]} > > Nested parens with #b and #c are a bit iffy though, e.g., the values > of $match[3] and beyond here are not what you might expect. > > On Mon, Sep 12, 2022 at 11:07 PM Lawrence Vel=C3=A1zquez > wrote: > > > > Your use case could theoretically be satisfied by an enhanced 'I' > > flag, so I don't think it deserves its own flag. > > > > On Tue, Sep 13, 2022, at 12:19 AM, Michele Venturi wrote: > > > How do you extend the I flag? > > > > I don't know. I am not familiar with the code base. > > Unfortunately this is non-trivial, because the value of the parameter > is scanned+replaced left-to-right and we don't know how many matches > there will be, to count backward. > > If you want to simulate a negative 'I' flag, one way is to use an > array, splitting and joining on the substring to replace: > > N=3D-2 > foo=3DaXbXcXdXe > z=3D(${(s:X:)foo}) > print -- ${(j:X:)z[1,N-1]}Y${(j:X:)z[N,-1]} > > Which you can write as a single substitution if you want to obfuscate it: > > print -- ${${(A)z::=3D${(@s:X:)foo}}:+${(j:X:)z[1,N-1]}Y${(j:X:)z[N,-1]}} > Thanks for supporting my idea, even if unconsciously, that explains why we need a dedicated flag very well... Plus you seem quite good at non trivial tasks if you can write that monstrousity... Do you know instead what IS trivial? Counting, to know how many matches there are. There are many examples of supporting negative index, so I still don't see a reason why it is so difficult to do it... > --000000000000c5669105e89a9bf6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Wed, Sep 14, 2022, 01:11 Bart Schaefer <schaefer@brasslantern.com> wro= te:
On Sun, Sep 11, 2022 at 11:01 P= M Lawrence Vel=C3=A1zquez <larryv@zsh.org> wrote:
>
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0% foo=3DaXbXcXdXe
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0% print ${(*)foo/(#b)(*)X/$match[1]Y}=

That works for the last match, but not for the Nth-from-last.=C2=A0 (It
also works only in zsh 5.9+, before that you need extendedglob set.)

You can do it more generally like this:

N=3D0=C2=A0 # Number of X to skip over when counting from the right
print -- ${(*)foo/%(#b)(*)X(*(X*)(#c$N))/$match[1]Y$match[2]}

Nested parens with #b and #c are a bit iffy though, e.g., the values
of $match[3] and beyond here are not what you might expect.

On Mon, Sep 12, 2022 at 11:07 PM Lawrence Vel=C3=A1zquez <larryv@zsh.org= > wrote:
>
> Your use case could theoretically be satisfied by an enhanced 'I&#= 39;
> flag, so I don't think it deserves its own flag.
>
> On Tue, Sep 13, 2022, at 12:19 AM, Michele Venturi wrote:
> > How do you extend the I flag?
>
> I don't know.=C2=A0 I am not familiar with the code base.

Unfortunately this is non-trivial, because the value of the parameter
is scanned+replaced left-to-right and we don't know how many matches there will be, to count backward.

If you want to simulate a negative 'I' flag, one way is to use an array, splitting and joining on the substring to replace:

N=3D-2
foo=3DaXbXcXdXe
z=3D(${(s:X:)foo})
print -- ${(j:X:)z[1,N-1]}Y${(j:X:)z[N,-1]}

Which you can write as a single substitution if you want to obfuscate it:
print -- ${${(A)z::=3D${(@s:X:)foo}}:+${(j:X:)z[1,N-1]}Y${(j:X:)z[N,-1]}}

Thanks for supporting my idea, even if unconsciou= sly,
that explains why we need a dedicated flag very= well...

Plus you seem q= uite good at non trivial tasks if you can
write that= monstrousity... Do you know instead what IS trivial? Counting, to know how= many matches there are.

There are many examples of supporting negative index,
so I still don't see a reason why it is so difficult to do it...
--000000000000c5669105e89a9bf6--