Perl Regex匹配磁铁链路的Infohash
-
15-11-2019 - |
题
即时尝试使用Perls Regex从Torrent Magnet链接中提取信息哈希
磁铁链接看起来像:
磁体:
但有时它可以看起来像:
磁铁:?XT= URN:BTIH:8AC3731AD4B039C05393B5404AFA6E7397810B41
试图提取的部分是8AC3731AD4B039C05393B5404AFA6E7397810B41
我试图捕捉到第一个'&'的一切,或者如果它只包括infohash然后达到行的结尾,我试过了一对夫妇,但不能让它正常工作
我在下面只捕获第一个字符
if ($tmpVar =~ m/magnet\:\?xt=urn\:btih\:([[:alnum:]]+?)/i) {
$mainRes{'hash'} = $1;
}
.
我也尝试了捕获后添加&| $,但它只是出现错误
谢谢
解决方案
You could use:
/\burn:btih:([A-F\d]+)\b/i
Or if the hash is always 40 chars:
/\burn:btih:([A-F\d]{40})\b/i
其他提示
As you've already discovered, you don't want to use the ?
in your regular-expressions. Here's why:
The ?
in pattern+?
makes your regex "non-greedy", meaning it will try to use as few characters as possible while still matching the pattern you specify. So
"8AC3731AD4B039C05393B5404AFA6E7397810B41" =~ /(\w+?)/
just returns "8
" while
"8AC3731AD4B039C05393B5404AFA6E7397810B41" =~ /(\w+)/
returns the whole string.
if ($tmpVar =~ m/magnet:\?xt=urn:btih:([[:alnum:]]+)/i) {
$mainRes{'hash'} = $1;
}
This is why the gods of CPAN gave us URI, to parse out parts of URIs, which you can then parse with a regex.
#!/usr/bin/perl
use URI;
use URI::QueryParam;
use Data::Dumper;
my $u = URI->new( shift() );
my $xt = $u->query_form_hash->{xt};
my ($hash) = $xt =~ m{^urn:btih:(.*)$};
print "$hash\n";
Presuming your magnet URI on the command line.