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

Re: Example of use of (S) flag



On Mon, Dec 28, 2015 at 7:23 PM, Philippe Troin <phil@xxxxxxxx> wrote:
> On Wed, 2015-12-23 at 21:01 -0800, Bart Schaefer wrote:
>> Further note that (MS) is sort of a dumbed-down form of backreferences,
>> returning something similar to the value of $MATCH in an extendeglob
>> pattern that uses (#m) (except extendedglob is not needed).  E.g.:
>
> 8< snip >8
>
>> torch% echo ${(S)a/b*/x}
>> axcba
>> torch% echo ${(S)a//b*/x}
>> axcxa
>
> I don't understand why in these two examples the star doesn't match the
> rest of the string?  I'd expect:
>
> % echo $a
> abcba
> % echo ${(S)a/b*/x}
> ax
> % echo ${(S)a//b*/x}
> ax
>
> Since b* should match the entire bcba substring.
> What did I miss?

(S) also enables shortest possible match (like # does and ## doesn't).
% a=abiibybeebz
% echo ${a/b*b/x}
axz
% echo ${(S)a/b*b/x}
axybeebz
% echo ${(S)a//b*b/x}
axyxz
% echo ${a#*b}
iibybeebz
% echo ${a##*b}
z

  S   Search substrings as well as beginnings or ends; with # start
from the beginning and with % start from the end of the string. With
substitution via ${.../...} or ${...//...}, specifies non-greedy
matching, i.e. that the shortest instead of the longest match should
be replaced.

-- 
Mikael Magnusson



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