Save consistent Perl-Regex as a variable

I have a simple Perl regular expression that I need to save as a variable.

If I print it:

print($html_data  =~ m/<iframe id="pdfDocument" src=.(.*)pdf/g);

It prints what I want to save, but when trying to save it with:

$link = $html_data =~ m/<iframe id="pdfDocument" src=.(.*)pdf/g;

I return the value '1' as a value $link. I guess this is because he found a match of "1". But how do I save the contents of a match?

+3
source share
4 answers

The corresponding template subexpressions are stored in variables $1, $2etc. You can also get the whole matched pattern ( $&), but this is expensive and should be avoided.

, , ; , Perl.

+4

/g, . . .

my @links = $html_data =~ m/<iframe id="pdfDocument" src=.(.*)pdf/g;

:

my ($link) = $html_data =~ m/<iframe id="pdfDocument" src=.(.*)pdf/;

parens ( /g ). , m// .

+6

perlfunc:


.

, print m//, m//, (wantarray?) -
( m// 1 0
fail, , m//g ).

$link= m// ( ) .
, m// 1 (true) 0 (false).

0

I just wrote such code. This can help. It basically looks like yours, except for mine it has a few brackets.

my $path = `ls -l -d ~/`;
#print "\n path is $path";
($user) = ($path=~/\.*\s+(\w+)\susers/);

So yours from this example might be something like this if you are trying to save all this? I'm not sure, but you can use mine as an example. I store everything that is in (\ w +):

($link) = ($html_data =~ (m/<iframe id="pdfDocument" src=.(.*)pdf/g));
0
source

All Articles