3

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?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
chrstahl89
  • 580
  • 10
  • 21

4 Answers4

7

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • The /g in scalar context is just fine if you're trying to do the same thing it does. You don't need it to get the list of the captures though. – brian d foy Apr 11 '12 at 23:59
  • @brian d foy, I didn't say */g in scalar context* isn't useful. I didn't even say it wasn't useful here. I said *scalar context* isn't useful here. (Though it can still be done in scalar context: `my $link = $html_data =~ m/.../ ? $1 : undef;`) – ikegami Apr 12 '12 at 18:26
4

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
geekosaur
  • 59,309
  • 11
  • 123
  • 114
0

From 'perlfunc' docs:

print LIST
Prints a string or a list of strings.

So,print m//, where m// determines that the return value
wanted (wantarray?) is a list
(It appers m// without capture groups returns 1 or 0 match pass
or fail, where as m//g returns a list of matches).

and

$link = m// can only be scalar (as opposed to list) context.
So, m// returns match results 1 (true) or 0 (false).

0

I just wrote code like this. It may help. It's basically like yours except mine has a few more parentheses.

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

So yours from this example may be something like this if your trying to store the whole thing? I'm not sure but you can use mine as an example. I am storing whatever is in (\w+):

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