Save Matched Perl Regex as Variable
perl, regex, scripting
Solution
The matched subexpressions of a pattern are saved in variables `$1`, `$2`, etc. You can also get the entire matched pattern (`$&`) but this is expensive and should be avoided.
The distinction in behavior here, by the way, is the result of scalar vs. list context; you should get to know them, how they differ, and how they affect the behavior of various Perl expressions.
Problem
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?