[wellylug] Regex, replace stuff in command line help...
Grant McLean
grant at mclean.net.nz
Thu Jun 29 12:30:30 NZST 2006
On Thu, 2006-06-29 at 11:51 +1200, David Antliff wrote:
> On Thu, 29 Jun 2006, Grant McLean wrote:
> > An example may clarify:
> >
> > $ echo '<img src="x.gif" border="0">' | \
> > > perl -nle '/src="(.*?)"/ && print $1'
> > x.gif
> >
> > Versus, without the ?
> >
> > $ echo '<img src="x.gif" border="0">' | \
> > > perl -nle '/src="(.*)"/ && print $1'
> > x.gif" border="0
>
> Indeed, I am familiar with the non-greedy rule for ?. However, this
> example is not analogous to your prior regexp because it includes a
> suffix anchor (").
Ok, sorry for the misunderstanding. In your original suggestion, you
left out the anchors because you didn't know the context - that's
exactly what I did too. You assumed the OP would put something useful
after the .* just as I assumed they'd put something useful after the .*?
My point was simply that while .* and .*? are both useful in their own
ways, .*? is generally safer and should be preferred, with .* being
reserved for those cases where a greedy match is really required.
Cheers
Grant
More information about the wellylug
mailing list