PHP正則表達(dá)式核心技術(shù)完全詳解 第3節(jié)

?極客小俊
?一個(gè)專注于web技術(shù)的80后
你不用拼過聰明人,你只需要拼過那些懶人 你就一定會(huì)超越大部分人!

模式修正符 或者叫 模式單元 ?語(yǔ)法: /原子+元字符+量詞/模式修正符號(hào) 或者說 /正則表達(dá)式/模式修正符 ?在php中模式修正符也叫修飾符,包含:i、m、s、U、e ?模式修正符必須配合正則函數(shù)一起使用才有意義 例如: "/<img\ssrc=".?"\/>/iU"
模式修正符就是幾個(gè)字母
是對(duì)整個(gè)正則表達(dá)式調(diào)優(yōu)使用, 也可以說是對(duì)正則表達(dá)式功能的擴(kuò)展 例如: "/abc/" 只能匹配小寫字母 abc "/abc/i" 可以不區(qū)分大小寫匹配 ABC abc Abc ABc AbC 模式修正符在php手冊(cè)中查找如下位置:

常用模式修正符如下: i : 表示在和模式進(jìn)行匹配進(jìn)不區(qū)分大小寫、執(zhí)行對(duì)大小寫不敏感的匹配 m : 將字符串視為多行 ?視為多行后,任何一行都可以以什么開始或以什么結(jié)束 ^ 和 將字符串視為單行、如果沒有使用這個(gè)模式修正符號(hào)時(shí),元字符中的默認(rèn)不能匹配換行符號(hào)也就是說如果把一個(gè)字符串視為單行、那么這個(gè)換行符也就是一個(gè)普通符號(hào)了不代表?yè)Q行的意思了,所以也就可以匹配上換行符了這里字符串是雙引號(hào)哦表示模式中的空白忽略不計(jì)再說一次是正則模式中的空白不是字符串中的!正則表達(dá)式必須使用在替換字符串的函數(shù)中時(shí)才可以使用現(xiàn)在已經(jīng)廢棄必須以什么開頭一般都用了解必須以什么結(jié)尾一般都用 ?[了解] D: 必須以什么結(jié)尾 但是結(jié)尾字符串后必須是沒得東西!設(shè)置m修正符后會(huì)失效! U : 修正正則的貪婪模式 原因: 正則表達(dá)式的特點(diǎn):就是比較”貪婪“ ?.* .+ 所有字符都符合這個(gè)貪婪條件 修正貪婪如下方式:
可以一次使用一個(gè),每一個(gè)具一定的意義, 也可以連續(xù)使用多個(gè)
使用模式修正符號(hào) U
一種是使用?完成 ?.? ?.+? 注意: 如果兩種方式同時(shí)出現(xiàn)又會(huì)開啟了貪婪模式 例如都存在 .? /U 小提示:模式修正符不是所有語(yǔ)言都支持!
模式修正符案例 如下:
?$pattern='/abC/i';
?$string='abcABC';
?
?
?$pattern='/^w.+/im';
?$string='abcABCcccc
?world
?element what wrong?';
?
?
?$pattern='/^w.+/ims';
?$string='abcABCcccc
?world
?element what wrong?';
?
?
?$pattern='/this is php/ix';
?$string='thisisphp';
?
?
?$pattern='/this\s?is\s?php/i';
?$string='this is php';
?
?
?$pattern='/this is/AD';
?$string='this is php';
?
?
?$pattern='/^t.*/U';
?$string='this is php';
?
?
?$pattern='/^t.*?/U';
?$string='this is php';
?
?
?preg_match($pattern, $string,$arr);
?show($arr);
接下來 讓我們來做一些非常簡(jiǎn)單的正則小練習(xí)題吧!
練習(xí)1: 匹配用戶名必須是英文+數(shù)字 最長(zhǎng)不超過8位, 最小5位 如下:
?$string='wang12345';
?$pattern='/^[a-zA-Z0-9]{1}[a-zA-Z0-9]{4,7}/';
?
?preg_match($pattern, $string,$arr);
?show($arr);
練習(xí)2:匹配Email ?如下:
?$string='1562hejun466@qq.com';
?//$string='mousesportsjonas@163.com';
?//$string='mouses.ports.jonas@163.com';
?//$string='mousical@public.net';
?//$string='mousical@public';
?
?$pattern='/^(?:\w+\.)?(?:\w+\.)?\w+@[a-zA-Z0-9]+(?:\.(?:net|org|com))?$/';
?
?preg_match($pattern, $string,$arr);
?show($arr);
練習(xí)3:匹配一個(gè)HTTP URL的正則表達(dá)式 如下:
?/*
? * 匹配URL 例如:
? * http://www.baidu.com
? * http://baidu.com
? * baidu.com
? * zhidao.baidu.com 等等
? */
?$string='http://www.baidu.com';
?$string='https://baidu.com';
?$string='https://www.baidu.com';
?$string='baidu.com';
?$string='zhidao.baidu.com';
?$string='http://zhidao.baidu.com';
?$pattern='/^(?:http[s]?:\/\/(?:www\.)?)?(?:[a-zA-Z0-9]+\.)?[a-zA-Z0-9]+\.(?:com|net|org)$/';
?
?preg_match($pattern, $string,$arr);
?show($arr);
練習(xí)4:匹配手機(jī)號(hào)碼與座機(jī)電話號(hào)碼正則表達(dá)式 如下:
?$string='63839154';
?$string='023-63505008';
?$string='18723188548';
?$string='0371-60333332';
?$pattern='/^1[35678]{1}\d{9}|0[0-9]{2,3}\-\d{7,8}|\d{7,8}/';
?
?preg_match($pattern, $string,$arr);
?show($arr);
?
練習(xí)5 :匹配時(shí)光網(wǎng)的首頁(yè)中的所有圖片爬取出來 ?如下:
?$string=file_get_contents('http://www.mtime.com/');
?$pattern='/<img\ssrc=\".*?\"\/>/';
?preg_match_all($pattern, $string,$arrList);
?
?$patternReplace='/(?<=src=\").+?(?=\")/';
?foreach ($arrList[0] as $k=>$v){
? ? ?preg_match($patternReplace, $v,$arr);
? ? ?if(!empty($arr[0])){
? ? ? ? ?$url[]=$arr[0];
? ? ? ? ?$patternList[]='/(?<=src=\").+?(?=\")/';
? ? ? ? ?echo preg_replace($patternList,$url,$arrList[0][$k]).'<br>';
? ? }
?}
?
練習(xí)6:匹配將某個(gè)列表頁(yè)面中的標(biāo)題名稱全部取出來循環(huán)成表格
?//練習(xí)6:匹配將某個(gè)列表頁(yè)面中的標(biāo)題名稱全部取出來循環(huán)成表格
?
?$string=file_get_contents('http://www.cqepc.cn/');
?
?$pattern='/\<div class=\"left\">.+?\<\/div\>/s';
?
?preg_match_all($pattern, $string,$arr);
?
?echo '<h2 style="text-align:center;">重慶航天職大招生就業(yè)信息</h2>';
?
?echo '<table border="1" cellpadding="0" cellspacing="0" width="700px" style="border-collapse:collapse; margin:20px auto;">';
?foreach ($arr[0] as $k=>$v){
? ? echo '<tr>';
? ? echo '<td style="padding:5px;">'.$v.'</td>';
? ? echo '</tr>';
?}
?echo '</table>';
?

?
如果喜歡話請(qǐng) 點(diǎn)贊 ?投幣 ?收藏 一鍵三連 ?
大家的支持就是我堅(jiān)持下去的動(dòng)力!
不要忘了?? 關(guān)注 ??哦!
