Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: ${name/pattern/repl} with negated pattern



# schaefer@xxxxxxxxxxxxxxxx / 2014-05-15 08:45:50 -0700:
> On May 15,  2:26pm, Roman Neuhauser wrote:
> }
> }     > 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?
> 
> You've forgotten that parameter expansion is a pattern match rather than
> a filename glob and that slashes have no special significance in pattern
> matching, so ^x\/* is ^(x\/*) despite the "higher precedence" mention.

no, i know the manpage says / is only special in filename generation.
sorry for being so implicit with the question, i should know better.

why does it remove the initial "x" from "x/foo" and "x/baz"?

> } i hoped zsh would have a direct way to expand only those items of an
> } array that match a pattern, eg. ${@:/#sh\/*}.
> 
> Not sure what #sh\/* means (you can't start an extendedglob pattern with
> the "#" repetition operator, though for some reason parameter expansion
> doesn't gripe about it)

zshexpn(1):

    ${name/pattern/repl}
    ${name//pattern/repl}
      [...]
      The pattern may begin with a `#', in which case the pattern must
      match at the start of the string, or `%', in which case it must
      match at the end of the string, or `#%' in which case the pattern
      must match the entire string.

> but the (M) qualifier with the :# susbstition
> should do what you want:
> 
>     ${(M)@:#pattern}

exactly what i'm after, thank you!

-- 
roman



Messages sorted by: Reverse Date, Date, Thread, Author