* Testing whether a string matches any pattern from a list
@ 2024-11-15 0:12 Philippe Altherr
2024-11-15 5:48 ` Roman Perepelitsa
2024-11-15 10:29 ` Peter Stephenson
0 siblings, 2 replies; 4+ messages in thread
From: Philippe Altherr @ 2024-11-15 0:12 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 917 bytes --]
I have an array that contains a list of patterns and a string. I would like
to check whether the string matches any of the patterns in the array. For
example if patterns=("foo*" "*bar"), then a test for "foob", "obar", and
"foobar" should return true and one for "ooba" should return false.
I hoped that I could use the following:
if [[ -v patterns[(k)$string] ]]; then ... fi;
Unfortunately, the pattern matching effect of the subscript flag
<https://zsh.sourceforge.io/Doc/Release/Parameters.html#Subscript-Flags> (k)
only works for associative arrays, not for regular arrays. I fixed the
"problem" by turning my array of patterns into an associative array:
local -A patterns=("foo*" X "*bar" X)
It's not a big deal but it feels a bit unnecessarily complicated. Is there
a simpler way to achieve this?
If not, would it make sense to change (k) and (K) to also do pattern
matching on regular arrays?
Philippe
[-- Attachment #2: Type: text/html, Size: 1231 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Testing whether a string matches any pattern from a list
2024-11-15 0:12 Testing whether a string matches any pattern from a list Philippe Altherr
@ 2024-11-15 5:48 ` Roman Perepelitsa
2024-11-15 10:29 ` Peter Stephenson
1 sibling, 0 replies; 4+ messages in thread
From: Roman Perepelitsa @ 2024-11-15 5:48 UTC (permalink / raw)
To: Philippe Altherr; +Cc: zsh-users
On Fri, Nov 15, 2024 at 1:13 AM Philippe Altherr
<philippe.altherr@gmail.com> wrote:
>
> I have an array that contains a list of patterns and a string. I
> would like to check whether the string matches any of the patterns
> in the array. For example if patterns=("foo*" "*bar"), then a test
> for "foob", "obar", and "foobar" should return true and one for
> "ooba" should return false.
You can join the elements of the array with "|" as the separator, and
then match against it:
[[ $string == (${~${(j:|:)patterns}}) ]]
You could also restructure your code to have a single pattern to begin with:
pattern='(foo*|*bar)'
[[ $string == $~pattern ]]
Roman
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Testing whether a string matches any pattern from a list
2024-11-15 0:12 Testing whether a string matches any pattern from a list Philippe Altherr
2024-11-15 5:48 ` Roman Perepelitsa
@ 2024-11-15 10:29 ` Peter Stephenson
2024-11-15 10:35 ` Peter Stephenson
1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2024-11-15 10:29 UTC (permalink / raw)
To: zsh-users
> On 15/11/2024 00:12 GMT Philippe Altherr <philippe.altherr@gmail.com>
> wrote: I have an array that contains a list of patterns and a
> string. I would like to check whether the string matches any of the
> patterns in the array. For example if patterns=("foo*" "*bar"), then a
> test for "foob", "obar", and "foobar" should return true and one for
> "ooba" should return false.
>
> I hoped that I could use the following:
>
> if [[ -v patterns[(k)$string] ]]; then ... fi;
>
> Unfortunately, the pattern matching effect of the subscript flag
> (https://zsh.sourceforge.io/Doc/Release/Parameters.html#Subscript-Flags) (k)
> only works for associative arrays, not for regular arrays.
I think you might be hitting a slightly different problem, that the test
is taking place in a special context that doesn't do the pattern lookup.
This is rather obscure so probably not intentional.
At least, the following does work for me (and doesn't
seem to be dependent on any options I have):
% string=B\*
% print $signals[(k)$string]
BUS
See if this works:
local lookup=${patterns[(k)$string]}
if [[ -n $lookup ]]; then
# ...
fi
pws
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Testing whether a string matches any pattern from a list
2024-11-15 10:29 ` Peter Stephenson
@ 2024-11-15 10:35 ` Peter Stephenson
0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2024-11-15 10:35 UTC (permalink / raw)
To: zsh-users
> On 15/11/2024 10:29 GMT Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> > On 15/11/2024 00:12 GMT Philippe Altherr <philippe.altherr@gmail.com>
> > wrote: I have an array that contains a list of patterns and a
> > string. I would like to check whether the string matches any of the
> > patterns in the array. For example if patterns=("foo*" "*bar"), then a
> > test for "foob", "obar", and "foobar" should return true and one for
> > "ooba" should return false.
> >
> > I hoped that I could use the following:
> >
> > if [[ -v patterns[(k)$string] ]]; then ... fi;
> >
> > Unfortunately, the pattern matching effect of the subscript flag
> > (https://zsh.sourceforge.io/Doc/Release/Parameters.html#Subscript-Flags) (k)
> > only works for associative arrays, not for regular arrays.
>
> I think you might be hitting a slightly different problem, that the test
> is taking place in a special context that doesn't do the pattern lookup.
> This is rather obscure so probably not intentional.
>
> At least, the following does work for me (and doesn't
> seem to be dependent on any options I have):
>
> % string=B\*
> % print $signals[(k)$string]
> BUS
>
> See if this works:
>
> local lookup=${patterns[(k)$string]}
> if [[ -n $lookup ]]; then
> # ...
> fi
Actually, it's only -v that's special --- so you can simply do
if [[ -n ${patterns[(k)$string]} ]]; then
# ...
fi
pws
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-11-15 10:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-15 0:12 Testing whether a string matches any pattern from a list Philippe Altherr
2024-11-15 5:48 ` Roman Perepelitsa
2024-11-15 10:29 ` Peter Stephenson
2024-11-15 10:35 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).