Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Answer: How do I replace a URL with a clickable hyperlink?

( #145424=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How do I replace a URL with a clickable hyperlink? contributed by rob_au

There is a module specifically built for this task - URI::Find. eg.

#!/usr/bin/perl -Tw use URI; use URI::Find; use strict; my $text = "... long string with lots of URLs ..."; find_uris($text, sub { my ($find_uri, $orig_uri) = @_; my $uri = URI->new( $orig_uri ); $uri = $uri->canonical->as_string; return '<a href="' . $uri . '">' . $uri . '</a>'; }); print $text, "\n"; exit 0;

Note that the CPAN documentation for URI::Find is out-of-date with the newest version (0.04) exporting only the one function, find_uris, which takes two arguments, the string to be searched and a function reference.

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Comment on Answer: How do I replace a URL with a clickable hyperlink?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-07-07 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls