Line break interferes with regular expression capture

4

I need to capture some information from divs that are sometimes separated by line breaks, and this causes the (.*) operator to not capture.

For example, I have the regular expression:

<div class="search tooltip box-video" search="(.*)" title="(.*)">(.*)<a href="    (.*)"><img src="(.*)" class="capa-poster" \/><\/a><\/div>

And I want you to marry HTML:

<a href="revenge.html">
<img src="http://i.imgur.com/1leadWY.jpg"class="capa-poster" /></a></div>

For testing I posted here .

    
asked by anonymous 17.01.2015 / 21:27

1 answer

2

It sounds like you're using php is what the link in the question says. In this case it would be enough to add the s modifier in your regex, when using it with the metacharacter point . new lines are combined / married.

Your regex will look like this in php

$str ='<div class="search tooltip box-video" search="revenge" title="Revenge">                                 
<a href="revenge.html"><img src="http://i.imgur.com/1leadWY.jpg"class="capa-poster" /></a></div>';

$regex = '/<div class="search tooltip box-video" search="(.*)" title="(.*)">(.*)<a href="(.*)"><img src="(.*)" class="capa-poster" \/><\/a><\/div>/s';
preg_match_all($regex, $str, $ocorrencias);

echo '<pre>';
print_r($ocorrencias);

Example - phpfiddle

List of PCRE modifiers

    
17.01.2015 / 21:55