[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Sitting next to an R/E God



"Bill Jones" <bill@fccj.org> writes:

> >From: Derek Balling <dredd@megacity.org>
> >>s/It's/$1, I say, $1/
> > 
> > I truly believe you wanted :
> >
> > s/(It's)/\1, I say, \1/
> >
> > There. Didn't you?
> >
> > Or did I miss something in the way regexes work? :)
> >
> > (I'll feel really stupid if I'm wrong, since I sit next to Jeffrey Friedl
> > at work *grin*)
> >
> 
> :]
> 
> Well, if you sit next to him, ask him why I
> am getting the URL three times out of this
> mess:
> 
> 
> $_ = 'http://web.fccj.org/~wcjones/index.html';
> s{http:(.*?)}    {<A HREF="$&">$_</A>}gs;

IANAREG, but it does what you ask it to.  The first part matches
exactly `http:', since you're using non-greedy Kleene start (/.*?/,
not /.*/). So it replaces the `http:' part of the string with what you 
want, and leaves the rest of the URL as it was.

You *really* want greediness (or an anchor) here:

<tobler 111 [13:14] ~ >perl -de 42

Loading DB routines from perl5db.pl version 1
Emacs support available.

Enter h or `h h' for help.

main::(-e:1):   42
  DB<1> $_ = 'http://web.fccj.org/~wcjones/index.html';

  DB<2> s{http:(.*?)}    {<A HREF="$&">$_</A>}gs;

  DB<3> x $_
0  '<A HREF="http:">http://web.fccj.org/~wcjones/index.html</A>//web.fccj.org/~wcjones/index.html'
  DB<4> $_ = 'http://web.fccj.org/~wcjones/index.html';

  DB<5> s{http:(.*)}    {<A HREF="$&">$_</A>}gs;

  DB<6> p $_
<A HREF="http://web.fccj.org/~wcjones/index.html">http://web.fccj.org/~wcjones/index.html</A>

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            |ariels@compugen.co.il
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem)	\  NEW IMPROVED URL!
72 Pinhas Rosen St.    |Tel: +972-3-7658520 (Main office)`--------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555  http://3w.compugen.co.il/~ariels

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org