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

42,120

Solution 1

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 ) {
  ...
} 

Solution 2

@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.

Solution 3

You just need the global modifier /g at the end of the match. Then loop through until there are no matches remaining

my @matches;
while ($string =~ /\<img\ssrc\="(.*)"/g) {
        push(@matches, $1);
}

Solution 4

Use the /g modifier and list context on the left, as in

@result = $string =~ /\<img\ssrc\="(.*)"/g;
Share:
42,120
VolatileRig
Author by

VolatileRig

Currently Innovating at Hiya Inc. Super Important Legalese: I don't represent or speak on behalf of my employer or any of its subsidiaries, partners or affiliates. Anything posted here or on other sites by me are my own views/opinions, no one else's. Any code I write here or on other sites comes with no implicit or explicit guarantees, promises, license, or warranty and I'm not liable for any damages as a result of running that code (please inspect and use discretion when running ANYONE's code).

Updated on May 03, 2020

Comments

  • VolatileRig
    VolatileRig about 4 years

    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?