Regex stumper?

Ben Marcotte ben_a_marcotte at yahoo.com
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\@lclark.edu is my address. Email me there, or at
wwwadmin\@lclark.edu, but not at <a href=mailto:goaway\@lclark.edu>here</a>,
dude.";
$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 lclark.edu">karic at lclark.edu</a> is my address. Email me 
there, or at <a href="mailto:wwwadmin at lclark.edu">wwwadmin at lclark.edu</a>, but
not at <a href=mailto:goaway at lclark.edu>here</a>, dude.


--- Kari Chisholm <karic at lclark.edu> 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\@lclark.edu 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 lclark.edu>here</a>.
> 
> into this
> 
> 	Email me <a href=mailto: <a
> href=mailto:"karic at lclark.edu">karic at lclark.edu</a>>here</a>.
> 
> And that's no good.
> 
> Any suggestions?
> 
> -kari.

=====
---------------------------------------
Ben Marcotte <ben_a_marcotte at yahoo.com>
---------------------------------------

__________________________________________________
Do You Yahoo!?
Kick off your party with Yahoo! Invites.
http://invites.yahoo.com/
TIMTOWTDI



More information about the Pdx-pm-list mailing list