Re: alternation option end of string
"Mark J. Reed" <[email protected]>
| Newsgroups | gmane.comp.shells.zsh.devel |
|---|---|
| Message-ID | <CAA=-s3w2Ms1dF3KxGU3qzg-+QLazyQWZRxg5NoWsRpOYRC7FqA__10541.7307627917$1777852962$gmane$org@mail.gmail.com> |
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=, 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; do
IFS=, 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 PM Ray Andrews <[email protected]> wrote:
>
>
> On 2026-05-03 11:45, Bart Schaefer wrote:
> > On Sun, May 3, 2026 at 6:56 AM Ray Andrews <[email protected]>
> 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=success ||
> > out=failure) -- 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=5
> ... had a more compact syntax -- if not so what, it's not a problem,
> still, sometimes zsh offers these little shortcuts.
>
>
>
>
>
--
Mark J. Reed <[email protected]>