一、貪婪與非貪婪
什么叫貪婪,比如說要從字符串中<td>面包一</td><td>面包二</td>吃面包,本來你只可以吃面包一,可是你貪心,于是就把第一個<td>到最后一個</td>里面的兩個面包取出來了,你想多吃點,非貪婪也就是你不貪吃了,就只吃面包一。
我們來看看正則里面是怎么貪婪的
<?php$str = '<td>面包一</td><td>面包二</td>';preg_match('/<td>(.*)<//td>/',$str,$res);print_r($res);
結果:
Array
(
[0] => <td>面包一</td><td>面包二</td>
[1] => 面包一</td><td>面包二
)
0記錄的是整個字符,1表示的是第一次匹配。
怎么來限制貪婪?
<?php$str = '<td>面包一</td><td>面包二</td>';preg_match('/<td>(.*?)<//td>/',$str,$res);print_r($res);Array ( [0] => <td>面包一</td> [1] => 面包一 )
在修飾匹配次數的特殊符號后再加上一個 "?" 號,則可以使匹配次數不定的表達式盡可能少的匹配。
在PHP中還可以通過修飾符來實現,
<?php$str = '<td>面包一</td><td>面包二</td>';preg_match('/<td>(.*)<//td>/U',$str,$res);print_r($res);
結果和上面一樣。這就是修飾符U的作用
二、預搜索
預搜索是一個非獲取匹配,不進行存儲供以后使用。
1、正向預搜索 "(?=xxxxx)","(?!xxxxx)"
"(?=xxxxx)”:所在縫隙的右側,必須能夠匹配上 xxxxx 這部分的表達式,
<?php$str = 'windows NT windows 2003 windows xp';preg_match('/windows (?=xp)/',$str,$res);print_r($res);
結果:
Array
(
[0] => windows
)
這個是xp前面的windows,不會取NT和2003前面的。
格式:"(?!xxxxx)",所在縫隙的右側,必須不能匹配 xxxxx 這部分表達式
<?php$str = 'windows NT windows 2003 windows xp';preg_match_all('/windows (?!xp)/',$str,$res);print_r($res);
結果:
Array
(
[0] => Array
(
[0] => windows 這個是nt前面的
[1] => windows 這個是2003前面的
)
)
從這里可以看出,預搜索不進行存儲供以后使用。
與會存儲的對比下。
<?php$str = 'windows NT windows 2003 windows xp';preg_match_all('/windows ([^xp])/',$str,$res);print_r($res);
結果:
Array
(
[0] => Array 全部模式匹配的數組
(
[0] => windows N
[1] => windows 2
)
[1] => Array 子模式所匹配的字符串組成的數組,通過存儲取得。
(
[0] => N
[1] => 2
)
)
2、反向預搜索 "(?<=xxxxx)","(?<!xxxxx)"
"(?<=xxxxx)" :所在縫隙的 "左側”能夠匹配xxxxx部分。
<?php$str = '1234567890123456';preg_match('/(?<=/d{4})/d+(?=/d{4})/',$str,$res);print_r($res);
結果:
Array
(
[0] => 56789012
)
匹配除了前4個數字和后4個數字之外的中間8個數字
"(?<!xxxxx)":所在縫隙的“左側”不能匹配xxxx部分。
<?php$str = '我1234567890123456';preg_match('/(?<!我)/d+/',$str,$res);print_r($res);
結果:
Array
(
[0] => 234567890123456
)
三、preg和ereg的區別
PHP同時使用兩套正則表達式規則,一套是由電氣和電子工程師協會(IEEE)制定的POSIX Extended 1003.2兼容正則(事實上PHP對此標準的支持并不完善),另一套來自PCRE(Perl Compatible Regular Expression)庫提供PERL兼容正則。 PHP5.3開始POSIX被刪除了。
preg_match 比 ereg效率高。