(using lookbehind assertions, see perldoc perlre). This, however, will not replace the URL that starts in the very first character in $message (since it's not following no whitespace). We can replace that separately:$message =~ s#(?<=\s)(http://\S+)#<a href="$1">$1</a>#g;
URLs are, however, often followed by a punctuation sign, which unfortunately will be included in our link (as in "http://google.com, for example…"). To remove this effect, make URLs end with a letter:$message =~ s#^(http://\S+)#<a href="$1">$1</a>#; $message =~ s#(?<=\s)(http://\S+)#<a href="$1">$1</a>#g;
I could go on talking about funny characters and special cases, but I'll just stop here and suggest you use Regexp::Common:$message =~ s#^(http://\S+[a-z])#<a href="$1">$1</a>#; $message =~ s#(?<=\s)(http://\S+[a-z])#<a href="$1">$1</a>#g;
use Regexp::Common qw( URI ); $message =~ s#^($RE{URI}{HTTP})#<a href="$1">$1</a>#; $message =~ s#(?<=\s)($RE{URI}{HTTP})#<a href="$1">$1</a>#g;
In reply to Re: detecting URLs and turning them into links
by akho
in thread detecting URLs and turning them into links
by keiusui
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |