Re: alternation option end of string

"Mark J. Reed" <[email protected]> Sun, 3 May 2026 20:01:17 -0400
Newsgroups gmane.comp.shells.zsh.user
Message-ID <CAA=-s3w2Ms1dF3KxGU3qzg-+QLazyQWZRxg5NoWsRpOYRC7FqA@mail.gmail.com>
--000000000000f47e460650f2a15c
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

What you really want is to _remove_ whatever comes after either "cats" or,
if the thing after "cat" is not an s, "cat".

But if you're reading comma-separated fields, you can just use `read`:

    IFS=3D, read base idx comment <<<"$INPUT"

e.g.

for INPUT in file file, file,8 file,88 file,8,comment file,88,comment \
             file,8a file,a8 file,a8,comment file,8a,comment
file,8a,com,ment \
             file,8,com,ment file,8,comm,9 file,8,comm,9a file,8,comm,a9; d=
o
  IFS=3D, read base idx comment <<<"$INPUT"
  printf '\n%8s: %s\n' INPUT "$INPUT"
  printf '%8s:\t%s\n' base "$base"
  printf '%8s:\t%s\n' idx "$idx"
  printf '%8s:\t%s\n' comment "$comment"
done

   INPUT: file
    base: file
     idx:
 comment:

   INPUT: file,
    base: file
     idx:
 comment:

   INPUT: file,8
    base: file
     idx: 8
 comment:

   INPUT: file,88
    base: file
     idx: 88
 comment:

   INPUT: file,8,comment
    base: file
     idx: 8
 comment: comment

   INPUT: file,88,comment
    base: file
     idx: 88
 comment: comment

   INPUT: file,8a
    base: file
     idx: 8a
 comment:

   INPUT: file,a8
    base: file
     idx: a8
 comment:

   INPUT: file,a8,comment
    base: file
     idx: a8
 comment: comment

   INPUT: file,8a,comment
    base: file
     idx: 8a
 comment: comment

   INPUT: file,8a,com,ment
    base: file
     idx: 8a
 comment: com,ment

   INPUT: file,8,com,ment
    base: file
     idx: 8
 comment: com,ment

   INPUT: file,8,comm,9
    base: file
     idx: 8
 comment: comm,9

   INPUT: file,8,comm,9a
    base: file
     idx: 8
 comment: comm,9a

   INPUT: file,8,comm,a9
    base: file
     idx: 8
 comment: comm,a9


On Sun, May 3, 2026 at 4:23=E2=80=AFPM Ray Andrews <[email protected]>=
 wrote:

>
>
> On 2026-05-03 11:45, Bart Schaefer wrote:
> > On Sun, May 3, 2026 at 6:56=E2=80=AFAM Ray Andrews <rayandrews@eastlink=
.ca>
> wrote:
> >> It might not actually be the '$' just some mechanism for matching a
> >> string that must end after some substring.
> > That's not what you're asking for, tho.  You're asking for the entire
> > string to fail to match if there's anything after the substring, but
> > in the same pattern you want the substring itself to match so you can
> > extract it.  You can't simultaneously have a failure and a success.
> > That's why Roman's solution has (compare && out=3Dsuccess ||
> > out=3Dfailure) -- there's no single pattern expression that can do both
> > branches.  (There might be a perl expression that works, but I'm not
> > confident pcre is perl-ish enough to do it.)
> I thought the thing was logically possible because it can be
> articulated: 'cat' then either nothing || an 's' which can itself be
> followed by anything.  But if it's not doable, as I said, no matter cuz
> the functionality is there and I'd do it Roman's way anyway cuz it's
> self-explanatory.  Too much terseness already, so I wish for more
> terseness very lightly.  Sorta like a while back I was wondering if:
> (( x > 5 )) && x=3D5
> ... had a more compact syntax -- if not so what, it's not a problem,
> still, sometimes zsh offers these little shortcuts.
>
>
>
>
>

--=20
Mark J. Reed <[email protected]>

--000000000000f47e460650f2a15c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">What you really want is to _remove_ whatever comes after e=
ither &quot;cats&quot; or, if the thing after &quot;cat&quot; is not an s, =
&quot;cat&quot;.=C2=A0<div><br></div><div>But if you&#39;re reading comma-s=
eparated fields, you can just use `read`:</div><div><br></div><div><font fa=
ce=3D"monospace">=C2=A0 =C2=A0 IFS=3D, read base idx comment &lt;&lt;&lt;&q=
uot;$INPUT&quot;</font></div><div><br></div><div>e.g.</div><div><br></div><=
div><font face=3D"monospace">for INPUT in file file, file,8 file,88 file,8,=
comment file,88,comment \<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0file,8a file,a8 file,a8,comment file,8a,comment file,8a,com,ment \<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0file,8,com,ment file,8,comm=
,9 file,8,comm,9a file,8,comm,a9; do<br>=C2=A0 IFS=3D, read base idx commen=
t &lt;&lt;&lt;&quot;$INPUT&quot;<br>=C2=A0 printf &#39;\n%8s: %s\n&#39; INP=
UT &quot;$INPUT&quot;<br>=C2=A0 printf &#39;%8s:\t%s\n&#39; base &quot;$bas=
e&quot;<br>=C2=A0 printf &#39;%8s:\t%s\n&#39; idx &quot;$idx&quot;<br>=C2=
=A0 printf &#39;%8s:\t%s\n&#39; comment &quot;$comment&quot;<br>done</font>=
<br></div><div><br><font face=3D"monospace">=C2=A0 =C2=A0INPUT: file<br>=C2=
=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	<br>=C2=A0comment:	<br><b=
r>=C2=A0 =C2=A0INPUT: file,<br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =
=C2=A0idx:	<br>=C2=A0comment:	<br><br>=C2=A0 =C2=A0INPUT: file,8<br>=C2=A0 =
=C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	8<br>=C2=A0comment:	<br><br>=
=C2=A0 =C2=A0INPUT: file,88<br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =
=C2=A0idx:	88<br>=C2=A0comment:	<br><br>=C2=A0 =C2=A0INPUT: file,8,comment<=
br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	8<br>=C2=A0comment:=
	comment<br><br>=C2=A0 =C2=A0INPUT: file,88,comment<br>=C2=A0 =C2=A0 base:	=
file<br>=C2=A0 =C2=A0 =C2=A0idx:	88<br>=C2=A0comment:	comment<br><br>=C2=A0=
 =C2=A0INPUT: file,8a<br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0id=
x:	8a<br>=C2=A0comment:	<br><br>=C2=A0 =C2=A0INPUT: file,a8<br>=C2=A0 =C2=
=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	a8<br>=C2=A0comment:	<br><br>=C2=
=A0 =C2=A0INPUT: file,a8,comment<br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=
=A0 =C2=A0idx:	a8<br>=C2=A0comment:	comment<br><br>=C2=A0 =C2=A0INPUT: file=
,8a,comment<br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	8a<br>=
=C2=A0comment:	comment<br><br>=C2=A0 =C2=A0INPUT: file,8a,com,ment<br>=C2=
=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	8a<br>=C2=A0comment:	com,=
ment<br><br>=C2=A0 =C2=A0INPUT: file,8,com,ment<br>=C2=A0 =C2=A0 base:	file=
<br>=C2=A0 =C2=A0 =C2=A0idx:	8<br>=C2=A0comment:	com,ment<br><br>=C2=A0 =C2=
=A0INPUT: file,8,comm,9<br>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0=
idx:	8<br>=C2=A0comment:	comm,9<br><br>=C2=A0 =C2=A0INPUT: file,8,comm,9a<b=
r>=C2=A0 =C2=A0 base:	file<br>=C2=A0 =C2=A0 =C2=A0idx:	8<br>=C2=A0comment:	=
comm,9a<br><br>=C2=A0 =C2=A0INPUT: file,8,comm,a9<br>=C2=A0 =C2=A0 base:	fi=
le<br>=C2=A0 =C2=A0 =C2=A0idx:	8<br>=C2=A0comment:	comm,a9</font><br></div>=
<div><br></div></div><br><div class=3D"gmail_quote gmail_quote_container"><=
div dir=3D"ltr" class=3D"gmail_attr">On Sun, May 3, 2026 at 4:23=E2=80=AFPM=
 Ray Andrews &lt;<a href=3D"mailto:[email protected]">rayandrews@eastl=
ink.ca</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left=
:1ex"><br>
<br>
On 2026-05-03 11:45, Bart Schaefer wrote:<br>
&gt; On Sun, May 3, 2026 at 6:56=E2=80=AFAM Ray Andrews &lt;<a href=3D"mail=
to:[email protected]" target=3D"_blank">[email protected]</a>&gt;=
 wrote:<br>
&gt;&gt; It might not actually be the &#39;$&#39; just some mechanism for m=
atching a<br>
&gt;&gt; string that must end after some substring.<br>
&gt; That&#39;s not what you&#39;re asking for, tho.=C2=A0 You&#39;re askin=
g for the entire<br>
&gt; string to fail to match if there&#39;s anything after the substring, b=
ut<br>
&gt; in the same pattern you want the substring itself to match so you can<=
br>
&gt; extract it.=C2=A0 You can&#39;t simultaneously have a failure and a su=
ccess.<br>
&gt; That&#39;s why Roman&#39;s solution has (compare &amp;&amp; out=3Dsucc=
ess ||<br>
&gt; out=3Dfailure) -- there&#39;s no single pattern expression that can do=
 both<br>
&gt; branches.=C2=A0 (There might be a perl expression that works, but I&#3=
9;m not<br>
&gt; confident pcre is perl-ish enough to do it.)<br>
I thought the thing was logically possible because it can be <br>
articulated: &#39;cat&#39; then either nothing || an &#39;s&#39; which can =
itself be <br>
followed by anything.=C2=A0 But if it&#39;s not doable, as I said, no matte=
r cuz <br>
the functionality is there and I&#39;d do it Roman&#39;s way anyway cuz it&=
#39;s <br>
self-explanatory.=C2=A0 Too much terseness already, so I wish for more <br>
terseness very lightly.=C2=A0 Sorta like a while back I was wondering if:<b=
r>
(( x &gt; 5 )) &amp;&amp; x=3D5<br>
... had a more compact syntax -- if not so what, it&#39;s not a problem, <b=
r>
still, sometimes zsh offers these little shortcuts.<br>
<br>
<br>
<br>
<br>
</blockquote></div><div><br clear=3D"all"></div><div><br></div><span class=
=3D"gmail_signature_prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_s=
ignature">Mark J. Reed &lt;<a href=3D"mailto:[email protected]" target=3D=
"_blank">[email protected]</a>&gt;<br></div>

--000000000000f47e460650f2a15c--