Regular [removed]PCRE) for URL matching

2019-09-14 01:14发布

问题:

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 :-[

回答1:

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!


回答2:

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



标签: php regex url pcre