zsh-users
 help / color / mirror / code / Atom feed
* Useful functions for non-greedy matching
@ 2019-08-15 18:47 Sebastian Gniazdowski
  0 siblings, 0 replies; only message in thread
From: Sebastian Gniazdowski @ 2019-08-15 18:47 UTC (permalink / raw)
  To: Zsh Users

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-08-16  1:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-15 18:47 Useful functions for non-greedy matching Sebastian Gniazdowski

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).