Multiple regex using perl

Hi, this website has already helped me solve my perl problems several times. this is the first time I have to ask a question because I cannot find the answer on google or stack overflow.

What I'm trying to do is get the content between two words. But the patterns they need to match are changing. I am trying to get product details. Brand, description, name, etc. I tried to execute the regex after another, but unfortunately this does not work because $ 1 remains defined. Trying to undef the variable $ 1 gives me a read-only error message, which is logical. I will post my code below, maybe someone has an idea how to make it work.

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use IO::File;
use utf8;
my $nfh = IO::File->new('test.html','w');
my $site = 'http://www.test.de/dp/';
my $sku = '1550043196';
my $url = join('',$site,$sku);
my $content = get $url;
my $name = $1 if ($content =~ m{<span id="productTitle" class="a-size-large">(.*?)</span>}gism);
print "$name\n";

# My attempt of undefying 
#undef $1;

my $marke = $1 if ($content =~ m{data-brand="(.*?)"}gism);
print "$marke\n";

Any suggestions?

+4
1

-, , :

my $var = $val if( $some );

:

: my, , (, my $x, ...) undefined. my undef, , , - . . perl - Perl . .

m//, /g , , . , 27 , :

my ($some) = $str =~ m/...(...).../g;

:

use strict;
use warnings;

my $u="undefined";
my $str = q{some="string" another="one"};

#will match
my ($m1) = $str =~ m/some="(.*?)"/g;
print 'm1=', $m1 // $u, '= $1=', $1 // $u, "=\n";

#will NOT match
my ($m2) = $str =~ m/nothere="(.*?)"/g;
print 'm2=', $m2 // $u, '= $1=', $1 // $u, "=\n";

#will match another
my ($m3) = $str =~ m/another="(.*?)"/g;
print 'm3=', $m3 // $u, '= $1=', $1 // $u, "=\n";

:

m1=string= $1=string=
m2=undefined= $1=string=   #the $1 hold previously matched value
m3=one= $1=one=

, $1 , . :

, % + hash ($1, $2, $3 ..) , , . (. perlsyn.)

: Perl reset , , .

, $1, , :

use strict
use warnings;

my $u="undefined";
my $str = q{some="string" another="one"};
my($m1,$m2,$m3);

{($m1) = $str =~ m/some="(.*?)"/g;}
print 'm1=', $m1 // $u, '= $1=', $1 // $u, "=\n";

{($m2) = $str =~ m/nothere="(.*?)"/g;}
print 'm2=', $m2 // $u, '= $1=', $1 // $u, "=\n";

{($m3) = $str =~ m/another="(.*?)"/g;}
print 'm3=', $m3 // $u, '= $1=', $1 // $u, "=\n";

m1=string= $1=undefined=
m2=undefined= $1=undefined=
m3=one= $1=undefined=

PS: Perl, , / .

+4

All Articles