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

Re: alternation option end of string



On Sat, May 2, 2026 at 2:48 AM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> On Sat, May 2, 2026 at 6:45 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>>
>> I want 'cat' to print but only if it has nothing following it except 's'
>> in which case  'cats' will print irrespective of following characters.
>
> This should do it, if I understood your requirements correctly:
>
> ${${(M)string:#cat(|s*)}[1,4]}

Somewhat more general (doesn't need to know how many characters are in "cats"):

${${(M)string:#cats}:-${(M)string:#cat}}

The first expansion returns "cats" only if that is a prefix, otherwise
the second expansion returns "cat" or nothing.




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