From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Useful functions for non-greedy matching
Date: Thu, 15 Aug 2019 20:47:18 +0200 [thread overview]
Message-ID: <CAKc7PVAnhW2cF1YdTAv9zRdi9sG=+Nv-Ynn-H73=iSA+qgQSug@mail.gmail.com> (raw)
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
reply other threads:[~2019-08-16 1:13 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAKc7PVAnhW2cF1YdTAv9zRdi9sG=+Nv-Ynn-H73=iSA+qgQSug@mail.gmail.com' \
--to=sgniazdowski@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).