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

Re: Substitute the last match of a pattern during parameters expansion.



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