Regex stumper?

Ben Marcotte ben_a_marcotte at
Thu Jul 27 18:24:27 CDT 2000

I learn something new everyday.  Apparently, somewhere between the publishing
of the 2nd Ed Camel book and a recent version of perl (5.005_03), a handy regex
piece was added: the negative lookbehind assertion, (?<!pattern).  It can be
used to test whether a given pattern (of fixed length) can _not_ be found
before some other piece of regex.  Thus we can check for the lack of a mailto:
before an address.  However, you might need a recent version of perl to do
this.  Does anyone out there actually know which version of perl first
supported this feature?  The best way to know if your version supports it is to
try it out. So, here's the new snippet:

$text = "karic\ is my address. Email me there, or at
wwwadmin\, but not at <a href=mailto:goaway\>here</a>,
$text =~
s#(?<!mailto:)\b([\w\-]+\@[\w\-]+\.[\w\-]+)\b#<a href="mailto:$1">$1</a>#g;
print $text;

Which for me produced:

<a href="mailto:karic at">karic at</a> is my address. Email me 
there, or at <a href="mailto:wwwadmin at">wwwadmin at</a>, but
not at <a href=mailto:goaway at>here</a>, dude.

--- Kari Chisholm <karic at> wrote:
> Thanks.  Your one-liner is very helpful.
> The reason I led off with the \s instead of a more symmetrical \b is to
> be able to match these bare emails (and convert them to mailto href's),
> but to leave untouched any mailto's that are already hand-coded in the
> text.  
> The problem, then, is that there has to be a leading space in the text
> I'm evaluating.  If it's 
> 	$text = "karic\ is my email address.";
> then it doesn't match with the leading \s.  \b would solve the problem,
> but then it converts this:
> 	Email me <a href=mailto:karic at>here</a>.
> into this
> 	Email me <a href=mailto: <a
> href=mailto:"karic at">karic at</a>>here</a>.
> And that's no good.
> Any suggestions?
> -kari.

Ben Marcotte <ben_a_marcotte at>

Do You Yahoo!?
Kick off your party with Yahoo! Invites.

More information about the Pdx-pm-list mailing list