开发者

Regular expression (PCRE) for URL matching

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 i开发者_StackOverflow中文版s an URL or no.

So, what regex do you use in such issues?

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


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

  • URI::Find
  • URI::Find::Schemeless

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</a> is swell!


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&mode=module

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜