Line break interferes with regular expression capture

Asked

Viewed 400 times

4

I’m needing to capture some Divs information that are sometimes separated by breaking lines, and this makes the operator (.*) capture not.

For example, I have 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 published here.

  • Should match, revenge, revenge, \n, revenge.htmland http://i.imgur.com/1leadWY.jpg that would be it?

  • Yes @rray .......

1 answer

2


It seems that you are using php is what indicates the question link. In this case just add the modifier s in its regex, when using it with the dot metacharacter . 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.