URI :: Find ::Schemelessを次のテキストで試したとき:
これがURLと1つの裸のURLです
https:https://www.example.comとクエリを使用した別の
http://example.org/?test=one&another=2および括弧付きの別の
http://example.org/(9.3)
引用符で囲まれたもう1つの「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))
。