Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Testing whether a string matches any pattern from a list



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 (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



Messages sorted by: Reverse Date, Date, Thread, Author