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

Re: non-greedy matching?



Peter Stephenson (pws@xxxxxxx) wrote:
> Adam Spiers <adam@xxxxxxxxxx> wrote:
> > They are done in order to strip control characters from a prompt so
> > that its display width can be determined.  At first I thought that it
> > would surely be easy to avoid this, but I still haven't come up with a
> > quick replacement, since neither zsh nor sed seem to be able to do
> > non-greedy matching.
> 
> You can, it's in the manual.
> 
> % foo='%{one%}hello%{two%}'
> % print ${(S)foo//[%]\{*[%]\}}
> hello

*gobsmacked*

I think that's the least uncomfortable I've ever felt after being
RTFM'd, given that you're apparently the only -worker who remembered
if that flag's existence ;-)

Can I suggest that it be made slightly more self-evident in the
manual, for instance via this patch?  I would expect most people
searching for this feature in the manual to use the keyword `greedy'.

Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.26
diff -u -r1.26 expn.yo
--- Doc/Zsh/expn.yo	2001/03/12 17:39:24	1.26
+++ Doc/Zsh/expn.yo	2001/03/21 22:59:30
@@ -779,8 +779,8 @@
 Search substrings as well as beginnings or ends; with tt(#) start
 from the beginning and with tt(%) start from the end of the string.
 With substitution via tt(${)...tt(/)...tt(}) or
-tt(${)...tt(//)...tt(}), specifies that the shortest instead of the
-longest match should be replaced.
+tt(${)...tt(//)...tt(}), specifies non-greedy matching, i.e. that the
+shortest instead of the longest match should be replaced.
 )
 item(tt(I:)var(expr)tt(:))(
 Search the var(expr)th match (where var(expr) evaluates to a number).

> I just realised: //% means match only at the end.
>   % print ${(S)foo//\\%\{*[%]\}}
> also works.

Is there no limit to what zsh can do? :-)



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