简体   繁体   中英

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

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</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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM