Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture multiple matches from the same Perl regex?

Tags:

string

regex

perl

I'm trying to parse a single string and get multiple chunks of data out from the same string with the same regex conditions. I'm parsing a single HTML doc that is static (For an undisclosed reason, I can't use an HTML parser to do the job.) I have an expression that looks like:

$string =~ /\<img\ssrc\="(.*)"/; 

and I want to get the value of $1. However, in the one string, there are many img tags like this, so I need something like an array returned (@1?) is this possible?

like image 374
VolatileRig Avatar asked May 21 '10 18:05

VolatileRig


People also ask

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

How do I match a pattern in Perl?

m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.

How do you match expressions in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


2 Answers

As Jim's answer, use the /g modifier (in list context or in a loop).

But beware of greediness, you dont want the .* to match more than necessary (and dont escape < = , they are not special).

while($string =~ /<img\s+src="(.*?)"/g ) {   ... }  
like image 176
leonbloy Avatar answered Sep 18 '22 22:09

leonbloy


@list = ($string =~ m/\<img\ssrc\="(.*)"/g); 

The g modifier matches all occurences in the string. List context returns all of the matches. See the m// operator in perlop.

like image 35
Robert Wohlfarth Avatar answered Sep 19 '22 22:09

Robert Wohlfarth