Home: Perl Programming Help: Regular Expressions:
URL's



parham_m_s
journeyman

Apr 25, 2001, 1:46 PM


Views: 3505
URL's

is there a snippet of code that replaces _any_ string url with actual link urls?



localfilmmaker
stranger

May 8, 2001, 11:11 AM


Views: 3483
Re: URL's

You can try this:

Code
my $text = 'this is text with a link to http://www.perl.com and to www.perldoc.com'; 

$text =~ s!(^|\s)(http://\S+)!<a href="$2">$2</a>!ig; # get all 'http://' links
$text =~ s!(^|\s)(www\.\w+\.\S+)!<a href="http://$2">$2</a>!ig; # get anything that simply starts 'www.'

print $text;

This seems to work, but you may try tweeking it to make it more robust.


-localfilmmaker
Spencer Christensen
spencer@mecworks.com