Regarding regular lazy mode and greedy mode

Lazy mode and greedy mode, literally, are one very lazy and the other very greedy.

Just one more question mark in the writing

<?php
$str='<p>asdfasdfs</p>dsfgsdftg<p>asdfasdfs</p>sdf<p>asdfasdfs</p>sdf<p>asdfasdfs</p>';

/*贪婪模式*/
if(preg_match_all('/<p>.*<\/p>/', $str, $m1)){
	var_dump($m1);
}

/*懒惰模式*/
if(preg_match_all('/<p>.*?<\/p>/', $str, $m2)){
	var_dump($m2);
}

Get the following results, the greedy mode will match as long as possible, and the lazy mode will match

Guess you like

Origin blog.csdn.net/weixin_43932088/article/details/88013521