Rodster001 has asked for the wisdom of the Perl Monks concerning the following question:
Using the following text:
I want to produce this:http://www.google.com <a href="http://www.google.com">fail a match here</a>
But, I am struggling a bit. This is what I have so far:<a href="http://www.google.com">http://www.google.com</a> <a href="http://www.google.com">fail a match here</a>
But, it is not quite doing what I want, it is still matching the second line even with the lookahead of (?!"|<) I am guessing I can't use | to mean "or" here.my $text = qq~ http://www.google.com <a href="http://www.google.com">fail a match here</a> ~; $text =~ s#(http://[^\s]*)(?!["|<])#<a href="$1">$1</a>#gsi; print $text;
I want my regex to "find any instance of a url not followed by a quote or a carrot" which will replace all urls with clickable links (except those that already have anchor tags around them). Different approaches to this (instead of the lookahead) are fine with me.
Thanks!
Update
This does what I need:
I gave up on the lookahead and just used a character class before the match. I hate giving up on something like that, but oh well :) thanks for all your input (Tokenize will come in handy ikegami, thanks!)s#[^">](http://[\S]*)# <a href="$1">$1</a>#gsi;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex: Lookahead
by ikegami (Patriarch) on May 11, 2009 at 22:54 UTC | |
|
Re: Regex: Lookahead
by Your Mother (Archbishop) on May 11, 2009 at 23:00 UTC | |
|
Re: Regex: Lookahead
by codeacrobat (Chaplain) on May 11, 2009 at 22:54 UTC | |
by Rodster001 (Pilgrim) on May 12, 2009 at 15:35 UTC | |
by ikegami (Patriarch) on May 12, 2009 at 16:34 UTC | |
|
Re: Regex: Lookahead
by JavaFan (Canon) on May 12, 2009 at 07:29 UTC | |
by Rodster001 (Pilgrim) on May 12, 2009 at 15:33 UTC | |
by ikegami (Patriarch) on May 12, 2009 at 15:44 UTC | |
|
Re: Regex: Lookahead
by ikegami (Patriarch) on May 12, 2009 at 15:56 UTC | |
by Your Mother (Archbishop) on May 12, 2009 at 17:00 UTC | |
by Rodster001 (Pilgrim) on May 12, 2009 at 16:00 UTC |