问题描述
输入:我们得到一些纯文本作为输入字符串,我们必须使用 <a href={url}>{url></a>
突出显示所有 URL.
The input: we get some plain text as input string and we have to highlighight all URLs there with <a href={url}>{url></a>
.
一段时间以来,我使用了取自 http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/,我修改了几次,但它是为另一个问题而构建的 -检查整个输入字符串是否为 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.
那么,您在此类问题中使用什么正则表达式?
So, what regex do you use in such issues?
UPD:如果答案与 php 相关就好了 :-[
UPD: it would be nice if answers were related to php :-[
推荐答案
看看 CPAN 上可用的几个模块:
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).
例如:
#! /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;
输出:
<a href="http://stackoverflow.com/users/251311/zerkms">http://stackoverflow.com/users/251311/zerkms</a> is swell!
这篇关于用于 URL 匹配的正则表达式 (PCRE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!