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.