php 正則預搜尋

2021-08-29 04:37:11 字數 1097 閱讀 8001

1、正向預搜尋  "(?=***xx)","(?!***xx)"

"(?=***xx)」:所在縫隙的右側,必須能夠匹配上 ***xx 這部分的表示式,

<?php

$str = 'windows nt windows 2003 windows xp';

preg_match('/windows (?=xp)/',$str,$res);

print_r($res);

結果:array

([0] => windows

)這個是xp前面的windows,不會取nt和2003前面的。

格式:"(?!***xx)",所在縫隙的右側,必須不能匹配 ***xx 這部分表示式

<?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、反向預搜尋  "(?<=***xx)","(? 56789012

)匹配除了前4個數字和後4個數字之外的中間8個數字

"(? 234567890123456

php 正則搜尋和替換 preg replace

preg replace 執行乙個正規表示式的搜尋和替換 方法說明 preg replace pattern replacement subject limit count 搜尋subject中匹配pattern的部分,以replacement進行替換。limit count 引數可有可無 limi...

正規表示式 預搜尋(零寬斷言)詳解

零寬斷言的意思是 匹配寬度為零,滿足一定的條件 斷言 我也不知道這個詞語是那個王八蛋發明的,簡直是太拗口了。零寬斷言用於查詢在某些內容 但並不包括這些內容 之前或之後的東西,也就是說它們像 b 這樣的錨定作用,僅僅用於指定乙個位置,不參與內容匹配,這個位置應該滿足一定的條件 即斷言 因此它們也被稱為...

正規表示式中的反向預搜尋 下

為 複製 如下 程式目的,去掉路徑中的網域名稱 var str var reg1www.cppcns.com str.match reg1 alert str.replace regexp.4,這個用法在字串中只有乙個url時,是適用的,但是如果字串中包含多個網域名稱,例如 複製 如下 var st...