当我使用以下文本尝试URI::Find::Schemeless时:
这是一个 URL 和一个裸 URL
https:https://www.example.com 和另一个带有查询的
http://example.org/?test=one&another=2 和另一个带括号的
http://example.org/(9.3)
另一个出现在引号中的“http://www.example.net/s=1;q=5”
等 到 ftp 站点的链接:ftp://user@example.org/test/me
一个没有协议的 www.example.com 怎么样?
它搞砸了http://example.org/(9.3)
。因此,我在Regexp::Common的帮助下提出了以下建议:
#!/usr/bin/perl
use strict; use warnings;
use CGI 'escapeHTML';
use Regexp::Common qw/URI/;
use URI::Find::Schemeless;
my $heuristic = URI::Find::Schemeless->schemeless_uri_re;
my $pattern = qr{
$RE{URI}{HTTP}{-scheme=>'https?'} |
$RE{URI}{FTP} |
$heuristic
}x;
local $/ = '';
while ( my $par = <DATA> ) {
chomp $par;
$par =~ s/</</g;
$par =~ s/( $pattern ) / linkify($1) /gex;
print "<p>$par</p>\n";
}
sub linkify {
my ($str) = @_;
$str = "http://$str" unless $str =~ /^[fh]t(?:p|tp)/;
$str = escapeHTML($str);
sprintf q|<a href="%s">%s</a>|, ($str) x 2;
}
这适用于所示的输入。当然,生活从来没有你尝试过的那么容易(http://example.org/(9.3))
。