Perl — regex issue

2019-09-26 02:09发布

I have the following code to get a substring inside an string, I'm using regular expressions but they seem not to work properly. How can I do it?

I have this string:

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

and I want to get this substring:

transacciones+banco+de+bogota

The code:

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;

The errors:

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.

3条回答
Viruses.
2楼-- · 2019-09-26 02:46

You are using the wrong tool for the job. You can use the URI module and its URI::QueryParam module to extract the parameters:

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');

Output:

transacciones banco de bogota
查看更多
干净又极端
3楼-- · 2019-09-26 03:05

the second declaration of $line is erroneous, drop my:

$line =~ m/textolibre=(.*?)&translated/g;
查看更多
姐就是有狂的资本
4楼-- · 2019-09-26 03:07

Sorry but I've found the answer. At line 10 my $line =~ m/textolibre=(.*?)&translated/g; I'm declaring the same variable two times so that's why throws the errors. Thanks!

查看更多
登录 后发表回答