Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Linkifying URLs in plain text

by moritz (Cardinal)
on Nov 21, 2008 at 16:00 UTC ( [id://725164]=note: print w/replies, xml ) Need Help??


in reply to Linkifying URLs in plain text

This is the regex I use to recognize URLs in plain text:
use Regexp::Common qw(URI); my $re = qr/$RE{URI}{HTTP}(?:#[\w_%:-]+)?(?<![.,])/;

The negative look-ahead takes care that in http://example.com/something, the comma isn't treated as part of the URL (it is a valid part of the URL, but usually you don't want to include it nevertheless).

For adding the trailing / and extracting the host name you need a bit more logic, for which I'm too lazy right now to write. I think that Regexp::Common has an option to capture the domain name somehow, but I haven't investigated in that either.

I hope this is of interest nonetheless.

Replies are listed 'Best First'.
Re^2: Linkifying URLs in plain text
by ikegami (Patriarch) on Nov 21, 2008 at 20:05 UTC

    For adding the trailing / and extracting the host name you need a bit more logic

    URI's ->canonical and ->host will do that, respectively.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://725164]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found