zsh-users
 help / color / 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, back to index

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

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git