zsh-users
 help / color / mirror / code / Atom feed
* ${name/pattern/repl} with negated pattern
@ 2014-05-15 12:26 Roman Neuhauser
  2014-05-15 15:32 ` Peter Stephenson
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Roman Neuhauser @ 2014-05-15 12:26 UTC (permalink / raw)
  To: zsh-users

this snippet in zshcontrib(1):

    for i in "$@"; do
        if [[ $i == sh/* ]]; then
            [[ -n $s ]] && s=$s,
            s=${s}$i
        fi
    done

can be reduced to

    s=${(j:,:)${@/#(^sh)\/*}}

i *thought*, based on the man page, that this would work as well:

    s=${(j:,:)${@/#^sh\/*}}

alas:

    > a=(x/foo y/bar x/baz y/qux)
    > echo ${a/#(^x)\/*} # ok
    x/foo x/baz
    > echo ${a/#^x\/*} # wtf
    /foo /baz

what's happening here?

zshexpn(1) suggests parentheses are not required:

    ^x     (Requires EXTENDED_GLOB to be set.) Matches anything except
           the pattern x. This has a higher precedence than `/', so
           `^foo/bar' will search directories in `.' except `./foo' for
           a file named `bar'.

though this description of a ksh glob operator is suspicious:

    !(...) Match anything but the expression in parentheses.
           (Like `(^(...))'.)


while i'm here: the whole workaround is cumbersome, you need to negate
the pattern (might be difficult in some cases) and actually get as many
items back as are in the input array (thus the nested parameter
expansion, ${(j:,:)a/#(^x)\/*} produces "x/foo,,x/baz,").
i hoped zsh would have a direct way to expand only those items of an
array that match a pattern, eg. ${@:/#sh\/*}.

doable?

-- 
roman


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-05-16  8:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-15 12:26 ${name/pattern/repl} with negated pattern Roman Neuhauser
2014-05-15 15:32 ` Peter Stephenson
2014-05-15 18:18   ` Roman Neuhauser
2014-05-15 15:45 ` Bart Schaefer
2014-05-15 16:37   ` Roman Neuhauser
2014-05-15 17:16     ` Bart Schaefer
2014-05-15 18:39 ` Roman Neuhauser
2014-05-16  8:40   ` Peter Stephenson

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