Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Matched Perl Regex as Variable

I have a simple Perl regex 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 get back a '1' as the value of $link. I assume this is because it found '1' match. But how do I save the content of the match instead?

like image 651
chrstahl89 Avatar asked May 31 '26 11:05

chrstahl89


1 Answers

Note the /g to get all matches. Those can't possibly be put into a scalar. You need an array.

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

If you just want the first match:

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

Note the parens (and the lack of now-useless /g). You need them to call m// in list context.

like image 178
ikegami Avatar answered Jun 02 '26 05:06

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!