zsh-workers
 help / color / mirror / code / Atom feed
* Re: non-greedy matching?
       [not found] <20010321142303.B8924@thelonious.new.ox.ac.uk>
@ 2001-03-21 15:52 ` Peter Stephenson
  2001-03-21 16:02   ` Peter Stephenson
  2001-03-21 23:00 ` Adam Spiers
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2001-03-21 15:52 UTC (permalink / raw)
  To: Zsh hackers list

Adam Spiers <adam@spiers.net> 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

Don't ask me why you need to put the `%' in square brackets --- there may
be some stray (s)printf in the code, for example.  (Do tell me if you know
what it is.)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


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

* Re: non-greedy matching?
  2001-03-21 15:52 ` non-greedy matching? Peter Stephenson
@ 2001-03-21 16:02   ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2001-03-21 16:02 UTC (permalink / raw)
  To: Zsh hackers list

> % foo='%{one%}hello%{two%}'
> % print ${(S)foo//[%]\{*[%]\}}
> hello
> 
> Don't ask me why you need to put the `%' in square brackets --- there may
> be some stray (s)printf in the code, for example.  (Do tell me if you know
> what it is.)

I just realised: //% means match only at the end.
  % print ${(S)foo//\\%\{*[%]\}}
also works.  I always loathed that syntax: it's unmemorable and hacky (all
right, it's not on its own).

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


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

* Re: non-greedy matching?
       [not found] <20010321142303.B8924@thelonious.new.ox.ac.uk>
  2001-03-21 15:52 ` non-greedy matching? Peter Stephenson
@ 2001-03-21 23:00 ` Adam Spiers
  2001-03-22  0:09   ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Adam Spiers @ 2001-03-21 23:00 UTC (permalink / raw)
  To: Zsh hackers list

Peter Stephenson (pws@csr.com) wrote:
> Adam Spiers <adam@spiers.net> 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? :-)


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

* Re: non-greedy matching?
  2001-03-21 23:00 ` Adam Spiers
@ 2001-03-22  0:09   ` Bart Schaefer
  2001-03-22 10:32     ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2001-03-22  0:09 UTC (permalink / raw)
  To: Zsh hackers list

On Mar 21, 11:00pm, Adam Spiers wrote:
} Subject: Re: non-greedy matching?
}
} 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'.

I don't think I would have thought of searching for "greedy" ... but
then I obviously never thought of searching for it at all.

And having this as a parameter expansion flag doesn't help when it
comes to glob patterns, which is (I rationalize) why I never thought
of searching for it.

Anyone for adding another extendedglob flag?  (#g)?  (Maybe another
letter would be better, since the flag would turn -off- greediness.)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: non-greedy matching?
  2001-03-22  0:09   ` Bart Schaefer
@ 2001-03-22 10:32     ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2001-03-22 10:32 UTC (permalink / raw)
  To: Zsh hackers list

Bart wrote:
> And having this as a parameter expansion flag doesn't help when it
> comes to glob patterns, which is (I rationalize) why I never thought
> of searching for it.
> 
> Anyone for adding another extendedglob flag?  (#g)?  (Maybe another
> letter would be better, since the flag would turn -off- greediness.)

It's not currently done in the globbing code, it's done by looping in the
parameter code.  It could be tricky to add directly.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


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

end of thread, other threads:[~2001-03-22 10:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20010321142303.B8924@thelonious.new.ox.ac.uk>
2001-03-21 15:52 ` non-greedy matching? Peter Stephenson
2001-03-21 16:02   ` Peter Stephenson
2001-03-21 23:00 ` Adam Spiers
2001-03-22  0:09   ` Bart Schaefer
2001-03-22 10:32     ` 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).