How to have a variable as a regular expression in Perl

I think this question is repeated, but the search did not help me.

my $pattern = "javascript:window.open\('([^']+)'\);";
$mech->content =~ m/($pattern)/;
print $1;

I want to have external $patternin regular expression. How can i do this? Current returns:

Using the uninitialized value of $ 1 in print on main.pm line 20.

+3
source share
5 answers

$1was empty, so the match didn’t work out. I will be making a constant line in my example, about which I know that it will match the pattern.

qr, . , , $pattern open, m , . $1, $2 .. .

my $pattern = qr"javascript:window.open\('([^']+)'\);";
my $content = "javascript:window.open('something');";
my @results = $content =~ m/($pattern)/;
# expression return array
# (
#     q{javascript:window.open('something');'},
#     'something'
# )
+3

, :

my $pattern = "javascript:window.open\('([^']+)'\);";
my $regex   = qr/$pattern/;

, , , , :

(?-xism:javascript:window.open('([^']+)');)/

, , "open". , , . ,

javascript:window.open'fum';

javascript:window.open('fum');

, , , Perl "\(" - , "(", Perl, '(' . escape-, .

my $pattern = "javascript:window.open\\('([^']+)'\\);";
my $regex   = qr/$pattern/;

( :

(?-xism:javascript:window.open\('([^']+)'\);)

, , .

, .

if ( $mech->content =~ m/($pattern)/ ) { 
     print $1;
}

. , , . .. , , .

$mech->content =~ m/($pattern)/;
print $1 || 'UNDEF!';

, , :

my ( $open_arg ) = $mech->content =~ m/($pattern)/;
print $open_arg || 'UNDEF!';

$open_arg " " . , , .

, , , "". perl . , , , , , .

. , , , , , , , .

my $pattern = qr/javascript:window.open\('([^']+)'\);/;

, . , . , , ( , ).

+2

. , $1, , , , .

$mech->content =~ m/$pattern/;

$mech->content =~ m/(?:$pattern)/;

, .

, , .

+1

, , window.open , "", , :

javascript:window.open("http://www.javascript-coder.com","mywindow","status=1,toolbar=1");

, :

my $pattern = qr{
    javascript:window.open\s*
    \(
    ([^)]+)
    \)
}x;

print $1 if $text =~ /$pattern/;

$1 split /,/, $stuff ..

+1

, $1 - undefined. $1 - undefined, , . undefined, .

0

Source: https://habr.com/ru/post/1760084/


All Articles