-3

我有以下代码来获取字符串中的子字符串,我正在使用正则表达式,但它们似乎无法正常工作。我该怎么做?

我有这个字符串:

vlex.es/jurisdictions/ES/search?textolibre=transacciones+banco+de+bogota&translated_textolibre=,300,220,00:00:38,2,0.00%,38.67%,€0.00

我想得到这个子字符串:

transacciones+banco+de+bogota

编码:

open my $info, $myfile or die "Could not open $myfile: $!";

while (my $line = <$info>) {
    if ($line =~ m/textolibre=/) {
        my $line =~ m/textolibre=(.*?)&translated/g;
        print $1;
    }

    last if $. == 3521239;
}

close $info;

错误:

Use of uninitialized value $line in pattern match (m//) at classifier.pl line 10, <$info> line 20007.
Use of uninitialized value $1 in print at classifier.pl line 11, <$info> line 20007.
4

3 回答 3

5

您为这项工作使用了错误的工具。您可以使用URI模块及其URI::QueryParam模块来提取参数:

use strict;
use warnings;
use URI;
use URI::QueryParam;

my $str = "ivlex.es/jurisdictions/ES/search?textolibre=transacciones+banco+de+bogota&translated_textolibre=,300,220,00:00:38,2,0.00%,38.67%,0.00";

my $u = URI->new($str); 
print $u->query_param('textolibre');

输出:

transacciones banco de bogota
于 2013-08-21T11:15:17.823 回答
0

抱歉,我找到了答案。在第 10 行my $line =~ m/textolibre=(.*?)&translated/g;,我两次声明同一个变量,这就是引发错误的原因。谢谢!

于 2013-08-21T11:05:59.657 回答
0

的第二个声明$line是错误的, drop my

$line =~ m/textolibre=(.*?)&translated/g;
于 2013-08-21T11:02:04.883 回答