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.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, 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 26134 invoked from network); 13 Sep 2022 23:12:48 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 13 Sep 2022 23:12:48 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1663110768; b=f4CMvnKnKKzA+41p5pUUlKy9x5crTC3VWeoHBaA68hf+w5HrWEfADcxvXKuPZM/3ofOTswIv73 xiN03yQOijB1FGwqayguGahkBsHb0CFP1pOMxc0UBRvIfB19YO41ytVszV2+I2cvwGPPvEpuCj ZjxVkKLc3OkTKQF2B7/s8KWcfHvOPROVKjs0OzyKXUJe0XArEp2RYKRHH3ImBaMoOht219Awfp BxS23b60ZIAakRWCFsCriFF0ChD4PlYT731vnFhVwyk31ZJY20X0RYaYiqCpNx8uAozy2TZDqA Aks6kdlF6mxa57iLIVOm8if2iuwGA5QP6Ubxf34e7rFb4w==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f47.google.com) smtp.remote-ip=209.85.218.47; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1663110768; bh=Oxwa0JlVK4xqqiRGAMGHsONPbADBDBV+BelvhoM9xq4=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:Cc:To:Subject: Message-ID:Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature: DKIM-Signature; b=WEdfk0byixqtlM8ILvVmiIHtycJMvWJxp402wTl15zg6h0dgFpHSM8m9Kpw20hupoPWNWINBm6 7rOjn6rjl2B20hhN88QBClXYPGsHb+0v8pedUW6pgkWbWnRyshaPMvs0G/n+wXDlM2teSPV8K+ 6lyv2gJ5aT+pCr3+PpUkeCMcrvgK6pV2FsjgZHthdgtmLmsT1fNVlid3ANTLq/VGxUhoXTnNz+ EOy4FvDc6QBcUnX8e64kObbcdmN8tmedf5b9ipk9l8XwFQvoQyWdsNUnpOdy7NuTQ3f7Tm6cs0 PNXJ373z+EEbw4+tbLC52gujWjn30RQhCpI1PSc7Xp97DQ==; 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-Transfer-Encoding: Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=OhCrvoD48BuhEfZoVkv7VU7pG4blkE/qQc4F3cV2TaI=; b=XTVLneW4XfyAlRU2ECEc8HSwkU 6sOFaTOoiV7TlEZFByX3UxHMMPh7ZlxvrU1jl8iYtKKxKmWsShm/f3zTNOsN3vKBYt0SsZI/Tc5FH xN1wq3KVlNPPwQoFf+3GiJVBuSYRaPH2jvOc0idNcZ3wj6VOInHcR7g0CN4jpnXaem9L12Le4FosY EPqqKTXToAxLBC5mCBROWWFx+4WnbifUsv3myRcPU6YN7i5ni1LArcZQj5GrEN0bEY7WHVvUHt/Ka utgrgpox7F4yFwdOT2a7G3KbNvDyFtHS/jzVjpApeLydipCXMytXJA9xkinTKfx5cD8v+IF2o8sed 1Rq0S36g==; Received: from authenticated user by zero.zsh.org with local id 1oYF5C-000Fdo-Lp; Tue, 13 Sep 2022 23:12:46 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f47.google.com) smtp.remote-ip=209.85.218.47; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f47.google.com ([209.85.218.47]:41564) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1oYF42-000EwR-Ia; Tue, 13 Sep 2022 23:11:35 +0000 Received: by mail-ej1-f47.google.com with SMTP id gh9so30878478ejc.8 for ; Tue, 13 Sep 2022 16:11:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=OhCrvoD48BuhEfZoVkv7VU7pG4blkE/qQc4F3cV2TaI=; b=q4+7EG0tqrl5fmXLct/kSAMsduYLGoB7NTEFcz4QsbF8KnAHmJi6kM57YXzalb/u1h fK9JP9sL2n0BegSWLNhilKLbWY/7ICTkpD6xNsF7myitXZg4rgL1EN35/ppHHNxINgqR 9wyY0Ov4zvYkdHXtd8aqKmM1TLb/OYNvHpZxu4F0HSPPUxvIuorMTl2dYs5krBRoB5qq eTWJP/+FJm9Noco4VFVDjWkwmrPKVLkAtffA0INBmeO5pac4YrRI65axaMu++u9CtBdz HIqaYBVY0/bAHoskXCKZ/DcgPyKg657A5sUy66ZWghNz4J1r1UtTscrSo/nLUwuqUQUm fRpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=OhCrvoD48BuhEfZoVkv7VU7pG4blkE/qQc4F3cV2TaI=; b=U2bHPUseJ/Nucb+FwnFNjJc161OEX1lKH6vQO0r4ixZPvYcD68J1OkjI/++AhlDs/7 Vmo66MQX/7tlgHCeuMmv109GiYBtNhPY5KMH9y8GWfUxmR8YLyzAbA+UFcqbyJ2G+oUl hrYlbkJio/7OnT61GUg8zuse2ia6GGTdBDCrolGjcLzTQymlfZ0LCQU24UQKcB8BGeCf Rv1XlBZ5R94Sfs/maPpZdrd51IYtv+bLrcs59TL9KBoWtMdPylM/dvp2AX+btDSvOZ8c vikrtz5J+qV50Bri+yc6KohZk9mA8tTB9xBdRXK2HRXAoPcs6qbimt2se7vW7tFYuinl dMwA== X-Gm-Message-State: ACgBeo3gnzWDO+QpCYmfMJ/WE/D3z7fQwuNqgvhx386A/uXvrr56nOmP WSUrrj34AreqVvMqVqR1RH1LpYbMvYqrj1hMGPV8R9D+yVYx5w== X-Google-Smtp-Source: AA6agR7rtd6c8OP+Ognmorfd6ewsz/xOqh4cOyvmfXBDX/FZUmVTz5w5Rv6qLnwpb55mWj4B2lLKhCn0vq34kdOB91U= X-Received: by 2002:a17:906:fe09:b0:73d:90ae:f801 with SMTP id wy9-20020a170906fe0900b0073d90aef801mr22422598ejb.699.1663110693876; Tue, 13 Sep 2022 16:11:33 -0700 (PDT) MIME-Version: 1.0 References: <4844b850-2ca6-4c90-95ae-aae00dc339cf@www.fastmail.com> In-Reply-To: <4844b850-2ca6-4c90-95ae-aae00dc339cf@www.fastmail.com> From: Bart Schaefer Date: Tue, 13 Sep 2022 16:11:22 -0700 Message-ID: Subject: Re: Substitute the last match of a pattern during parameters expansion. To: zsh-users@zsh.org Cc: Michele Venturi Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 28067 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: On Sun, Sep 11, 2022 at 11:01 PM Lawrence Vel=C3=A1zquez w= rote: > > % 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 w= rote: > > 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]}}