tags:

views:

63

answers:

2

The input: we get some plain text as input string and we have to highlighight all URLs there with <a href={url}>{url></a>.

For some time I've used regex taken from http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/, which I modified several times, but it's built for another issue - to check whether the whole input string is an URL or no.

So, what regex do you use in such issues?

UPD: it would be nice if answers were related to php :-[

A: 

For Perl, I usually use one of the modules defining common regex, Regexp::Common::URI::*. You might find a good regexp for you in the sources of those modules.

http://search.cpan.org/search?query=Regexp%3A%3ACommon%3A%3AURI&amp;mode=module

jkramer
A: 

Take a look at a couple of modules available on CPAN:

where the latter is a little more forgiving. The regular expressions are available in the source code (the latter's, for example).

For example:

#! /usr/bin/perl

use warnings;
use strict;

use URI::Find::Schemeless;

my $text = "http://stackoverflow.com/users/251311/zerkms is swell!\n";

URI::Find::Schemeless
  ->new(sub { qq[<a href="$_[0]">$_[0]</a>] })
  ->find(\$text);

print $text;

Output:

<a href="http://stackoverflow.com/users/251311/zerkms">http://stackoverflow.com/users/251311/zerkms&lt;/a> is swell!
Greg Bacon