Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Testing whether a string matches any pattern from a list
- X-seq: zsh-users 30108
- From: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- To: Philippe Altherr <philippe.altherr@xxxxxxxxx>
- Cc: zsh-users@xxxxxxx
- Subject: Re: Testing whether a string matches any pattern from a list
- Date: Fri, 15 Nov 2024 06:48:49 +0100
- Archived-at: <https://zsh.org/users/30108>
- In-reply-to: <CAGdYchuk6nmJcORxZaNxrbjXWm6=XrqS0qNgdgoEqSJwEnxzGw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CAGdYchuk6nmJcORxZaNxrbjXWm6=XrqS0qNgdgoEqSJwEnxzGw@mail.gmail.com>
On Fri, Nov 15, 2024 at 1:13 AM Philippe Altherr
<philippe.altherr@xxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author