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

Substitute the last match of a pattern during parameters expansion.



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?

Hard-coding a number like in ${(I:2:)str/X/Y}
is usually not a good idea, don’t you agree?
Other possibilities have similar down sides.

So is there an easier way to get the desired
result or should it be added to the software
and how complicated would be to change it?

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…

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

What do you think about this whole matter?
What should I learn to find out how to do it?
Your thoughts on the topic are appreciated…


Michele Venturi
about.me/dardo82


Messages sorted by: Reverse Date, Date, Thread, Author