Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Substitute the last match of a pattern during parameters expansion.
- X-seq: zsh-users 28062
- From: Lawrence Velázquez <larryv@xxxxxxx>
- To: "Michele Venturi" <dardo82@xxxxxxxxx>
- Cc: zsh-users@xxxxxxx
- Subject: Re: Substitute the last match of a pattern during parameters expansion.
- Date: Mon, 12 Sep 2022 02:01:13 -0400
- Archived-at: <https://zsh.org/users/28062>
- Feedback-id: iaa214773:Fastmail
- In-reply-to: <CA+Ds4NsnE6HvV_Kf59KO5_=8Jb4STxwAzL8x+EUq1aiQdB91ZA@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author