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

Re: The ~ parameter expansion flag: bug or misunderstanding

On Sep 3, 10:58am, Clint Hepner wrote:
} My reading of the ~ flag leads me to believe that you can replace the
} literal string with a pattern, so that
} % print -l ${(~s.[12].):-a1b2c}
} a
} b
} c

No, that's not what the flag means.  It means that after the expansion
of the parameter has been completed, any pattern characters inserted
into the result are active (i.e., not considered quoted).  It doesn't
otherwise change the operation of the other flags themselves.

This doesn't have any effect for the (s) flag because (s) removes
characters from the result, rather than inserting them.

The purpose of the (~) flag is really to work with the (j) flag, so
that you can create a pattern from an array without having any other
pattern characters in the values of the array also become active.
It also works with (l) and (r), though the uses for treating padding
as pattern characters are pretty obscure.

To use a pattern for processing the value during expansion, you need
to use the trailing // notation, e.g.

    % print -l ${=${:-a1b2c}//[12]/ }


    % print -l ${(s:1:)${:-a1b2c}//[12]/1}

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