Re: alternation option end of string
Bart Schaefer <[email protected]> Sat, 2 May 2026 09:26:59 -0700
| Newsgroups | gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <CAH+w=7ZR=Xhq0iYW3SafTyH03AaJCMnDHgMv+wzB3Le=yK9vMw@mail.gmail.com> |
On Sat, May 2, 2026 at 2:48 AM Roman Perepelitsa <[email protected]> wrote: > > On Sat, May 2, 2026 at 6:45 AM Ray Andrews <[email protected]> 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.