zsh-users
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: "Michele Venturi" <dardo82@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: Substitute the last match of a pattern during parameters expansion.
Date: Mon, 12 Sep 2022 02:01:13 -0400	[thread overview]
Message-ID: <a0ea450b-eb84-47d1-a4ae-bd0bfbef51ea@www.fastmail.com> (raw)
In-Reply-To: <CA+Ds4NsnE6HvV_Kf59KO5_=8Jb4STxwAzL8x+EUq1aiQdB91ZA@mail.gmail.com>

On Mon, Sep 12, 2022, at 12:20 AM, Michele Venturi wrote:
> Hello, I have a problem, can you help me?
> I would like to substitute the last match of
> a pattern during parameters expansion…
> The string aXbXc should become aXbYc;
> I have not found a direct and general way
> of doing it, but there are some solutions…
>
> The problem is that they are cumbersome:
> ${str%X*}Y${str##*X} is too convoluted for
> such a simple operation in my opinion, no?

Lots of tasks could be reasonably described as "simple".  The shell
cannot provide succinct solutions to all of them.

> So is there an easier way to get the desired
> result

Here's one approach.

	% foo=aXbXcXdXe                    
	% print ${(*)foo/(#b)(*)X/$match[1]Y}
	aXbXcXdYe

> I would suggest a new flag like % or $ for it.
> I wonder if the syntax ${(%)str/X/Y} is going
> to conflict with other functionalities of ZSH…

There is already a '%' flag.  I do not think this merits a new one.

> Obviously adding negative indexes support
> to the I flag would be very good but verbose
> for the most common use case of that, right?

Extending the 'I' flag in this way would be ideal, if possible.

-- 
vq


  reply	other threads:[~2022-09-12  6:02 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-12  4:20 Michele Venturi
2022-09-12  6:01 ` Lawrence Velázquez [this message]
2022-09-13  4:19   ` Michele Venturi
2022-09-13  6:06     ` Lawrence Velázquez
2022-09-13 23:11       ` Bart Schaefer
2022-09-14  3:20         ` Michele Venturi
2022-09-14 18:56           ` Bart Schaefer
2022-09-14 20:06             ` Ray Andrews
2022-09-14 21:59             ` Michele Venturi
2022-09-14 22:04               ` Bart Schaefer
2022-09-14 22:20                 ` Michele Venturi
2022-09-15  0:00                   ` Alex Satrapa
2022-09-15  0:29               ` Lawrence Velázquez
2022-09-15  1:45                 ` Michele Venturi
2022-09-15 12:01                   ` Roman Perepelitsa
2022-09-15 23:29                     ` Michele Venturi
2022-09-16  6:37                       ` Roman Perepelitsa
2022-09-16  7:52                         ` Michele Venturi
2022-09-16  7:57                           ` Roman Perepelitsa
2022-09-16  8:19                             ` Michele Venturi
2022-09-16  8:22                               ` Roman Perepelitsa
2022-09-16  8:33                                 ` Michele Venturi
2022-09-16  8:36                                   ` Roman Perepelitsa
2022-09-16  9:08                                     ` Michele Venturi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a0ea450b-eb84-47d1-a4ae-bd0bfbef51ea@www.fastmail.com \
    --to=larryv@zsh.org \
    --cc=dardo82@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).