Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Useful functions for non-greedy matching
- X-seq: zsh-users 24144
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Useful functions for non-greedy matching
- Date: Thu, 15 Aug 2019 20:47:18 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=nNE9XdGGEAbt0LQnNKWUe79mHNCMZtnaLb6F0kwUPj8=; b=jNtNFWtKo8mDo3K4ZwzssXYT19UFRsrw0ZO7EaL5uEici4ABzjI+1AFpuR3PPkKGwz 5Cv2AF4afq6UwLZTDADahdaqnf5jqQxt7rxTI6FMHbM+cRZxeAFYpcsD/SAeqZKBqOjk 6nrG9+uorC9MC1wkPT2fby2wGcQ1VmUCQfoz4xJjWQrrBkdJS10H0CJulO06RklDfveg 3ns1Q7DHI3UFvLZvRMqTHmaSxifHCpxXTHam+FJRGkNCl+ZnEfG8UrjlMhngQTRTIaIx M6RYS+EQwqz3IIkUcM+AkEceOSjdA2u5M8ldNb5v9dm+HoXABnulHoaYrl7XHsh6oNF7 HKAg==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
Hello,
Zsh supports non greedy matching only in substitutions like //, via
the (S) flag. This however turns out to be not that big problem thanks
to the following functions:
.smatch() {
local str="$1" pat="$2" retval=1
match=()
: ${(S)str/(#b)(${~pat})/${retval::=0}}
REPLY="${match[1]}"
return $retval
}
.smatches() {
local pat="${@[${#}]}" retval=1
local -a input
input=( "${@[1,${#}-1]}" ) reply=() match=()
: "${(S)input[@]//(#b)(${~pat})/${reply[${#reply}+1]::=${match[1]}}${retval::=0}}"
REPLY="${match[1]}"
return $retval
}
Example usage:
arr=( a1xx ayy a2xx )
if .smatches ${arr[@]} "a*x"; then
print -rl $reply
fi
Outout:
a1x
a2x
Gist for possible updates:
https://gist.github.com/psprint/a69177329d6d15bffdd320011aa208da
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
Messages sorted by:
Reverse Date,
Date,
Thread,
Author