i could match content between tr tags with this regex:
<tr\s+class='test'>((?!</tr>).)*</tr>
but if i put the star quantifiers inside the parenthesis right next to the dot metacharacters,they match only the whole pattern with capturing group empty.
$string = "<tr class='test'>
<td>test1</td>
</tr>
<div class='ignored' >text text</div>
<tr class='test'>
<td>test2</td>
</tr>";
preg_match_all("|<tr\s+class='test'>((?!</tr>).*)</tr>|si",$string,$matches);
print_r($matches);
i know what lookaround is but i'm not quite sure what exactly cause the difference. hope someone can shed some light on this. Thank you!
((?!</tr>).)*
The repetition is applied to ((?!</tr>).)
and there is a single .
and a single lookahead. Therefore, this will check each and every .
(at each repetition) and make sure they are not followed by </tr>
.
((?!</tr>).*)
This is actually (?!</tr>).*
in disguise. There is a single lookahead and a single .*
. The lookahead will check only the first .
, but not the others, which is why everything will be matched, unless the immediate dots after the lookahead matches </tr>
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments