[英]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>
. 输入:我们将一些纯文本作为输入字符串,并且必须使用
<a href={url}>{url></a>
那里的所有URL。
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. 一段时间以来,我一直使用来自http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/的正则表达式,我对其进行了多次修改,但它是为另一个而构建的问题-检查整个输入字符串是否为URL。
So, what regex do you use in such issues? 那么,您在此类问题中使用什么正则表达式?
UPD : it would be nice if answers were related to php :-[ UPD :如果答案与php有关,那就太好了:-[
Take a look at a couple of modules available on CPAN: 看一下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::*
. 对于Perl,我通常使用定义通用正则表达式的模块之一
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 http://search.cpan.org/search?query=Regexp%3A%3ACommon%3A%3AURI&mode=module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.