Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

string - Perl -- regex issue

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%,a??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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...