通常我們會這麼寫:
復制代碼 代碼如下:
$str = "http://www.baidu/.com?url=www.sina.com/";
preg_match("/http:(.*)com/", $str, $matches);
print_r($matches);
結果:
復制代碼 代碼如下:
Array ( [0] => http://www.baidu/.com?url=www.sina.com [1] => //www.baidu/.com?url=www.sina. )
非貪婪模式匹配:
復制代碼 代碼如下:
$str = "http://www.baidu/.com?url=www.sina.com/";
preg_match("/http:(.*?)com/", $str, $matches);
print_r($matches);
結果:
復制代碼 代碼如下:
Array ( [0] => http://www.baidu/.com [1] => //www.baidu/. )
簡單的說只要在一個字符後面跟上限定個數的特殊字符,匹配就是非貪婪模式了。小伙伴們是否理解了呢?