Re: alternation option end of string

Roman Perepelitsa <[email protected]>
Newsgroups gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user
Message-ID <CAN=4vMpz9buom+J_qcyv61X8BVV5CYZvb2qQuhyu6QNdg=VYnw@mail.gmail.com>
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.
> So 'cat' and 'cats' are the only outputs and
> 'catz' will print nothing but 'catszzzzz' will print 'cats'.  At the
> moment I'm doing this with two sequential tests and it works fine, but
> I'm wondering if they can be combined.
>

This should do it, if I understood your requirements correctly:

${${(M)string:#cat(|s*)}[1,4]}

The first expansion leaves cat(|s*) intact (cat, cats, catsz, catszz, etc.)
and turns everything else into nul. The second expansion extracts the first
4 characters, effectively dropping any potential suffix after cats.

Another option would be to capture the interesting part of the match
explicitly (requires extended_glob):

[[ $string == (#b)((cat)|(cats)*) ]] && out=$match[2]$match[3] || out=

Roman
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.