作者:極客小俊 一個專注于web技術的80后
我不用拼過聰明人,我只需要拼過那些懶人 我就一定會超越大部分人!
CSDN@極客小俊,原創文章, B站技術分享
B站視頻 : ?? Bilibili.com ??
個人博客: ?? cnblogs.com ??

我們接著上一節的內容繼續說正則..
模式修正符 或者叫 模式單元
語法: /原子+元字符+量詞/模式修正符號 或者說 /正則運算式/模式修正符
在php中模式修正符也叫修飾符,包含:i、m、s、U、e 模式修正符必須配合正則函式一起使用才有意義
例如: "/<img\ssrc="https://www.cnblogs.com/GeekerJun/p/.?"/>/iU"
1. 模式修正符就是幾個字母
2. 可以一次使用一個,每一個具一定的意義, 也可以連續使用多個
3. 是對整個正則運算式調優使用, 也可以說是對正則運算式功能的擴展
例如:
"/abc/" 只能匹配小寫字母 abc
"/abc/i" 可以不區分大小寫匹配 ABC abc Abc ABc AbC
模式修正符在php手冊中查找如下位置:

常用模式修正符如下:
i : 表示在和模式進行匹配進不區分大小寫、執行對大小寫不敏感的匹配
m : 將字串視為多行 視為多行后,任何一行都可以以什么開始或以什么結束 ^ 和 $
s : 將字串視為單行、 如果沒有使用這個模式修正符號時, 元字符中的"."默認不能匹配換行符號, 也就是說如果把一個字串視為單行、那么這個換行符也就是一個普通符號了,不代表換行的意思了 ,所以 . 也就可以匹配上換行符了! [這里字串是雙引號哦]
x : 表示模式中的空白忽略不計,再說一次是正則模式中的空白 不是字串中的!
e : 正則運算式必須使用在preg_replace替換字串的函式中時才可以使用 [現在已經廢棄]
A : 必須以什么開頭 一般都用^ [了解]
Z : 必須以什么結尾 一般都用$ [了解]
D: 必須以什么結尾 但是結尾字串后必須是沒得東西!設定m修正符后會失效!
U : 修正正則的貪婪模式
原因: 正則運算式的特點:就是比較”貪婪“ .* .+ 所有字符都符合這個貪婪條件
修正貪婪如下方式:
- 使用模式修正符號 U
- 一種是使用?完成 .? .+?
注意: 如果兩種方式同時出現又會開啟了貪婪模式 例如都存在 .? /U
小提示:模式修正符不是所有語言都支持!
模式修正符案例 如下:
$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);
接下來 讓我們來做一些非常簡單的正則小練習題吧!
練習1: 匹配用戶名必須是英文+數字 最長不超過8位, 最小5位 如下:
$string='wang12345';
$pattern='/^[a-zA-Z0-9]{1}[a-zA-Z0-9]{4,7}/';
preg_match($pattern, $string,$arr);
show($arr);
練習2:匹配Email 如下:
$string='[email protected]';
//$string='[email protected]';
//$string='[email protected]';
//$string='[email protected]';
//$string='mousical@public';
$pattern='/^(?:\w+\.)?(?:\w+\.)?\w+@[a-zA-Z0-9]+(?:\.(?:net|org|com))?$/';
preg_match($pattern, $string,$arr);
show($arr);
練習3:匹配一個HTTP URL的正則運算式 如下:
/*
* 匹配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);
練習4:匹配手機號碼與座機電話號碼正則運算式 如下:
$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);
練習5 :匹配時光網的首頁中的所有圖片爬取出來 如下:
$string=file_get_contents('http://www.mtime.com/');
$pattern='/<img\ssrc=https://www.cnblogs.com/".*?\"\/>/';
preg_match_all($pattern, $string,$arrList);
$patternReplace='/(?<=data-src=https://www.cnblogs.com/").+?(?=\")/';
foreach ($arrList[0] as $k=>$v){
preg_match($patternReplace, $v,$arr);
if(!empty($arr[0])){
$url[]=$arr[0];
$patternList[]='/(?<=src=https://www.cnblogs.com/").+?(?=\")/';
echo preg_replace($patternList,$url,$arrList[0][$k]).'<br>';
}
}
練習6:匹配將某個串列頁面中的標題名稱全部取出來回圈成表格
//練習6:匹配將某個串列頁面中的標題名稱全部取出來回圈成表格
$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;">重慶航天職大招生就業資訊</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>';

"點贊" "評論" "收藏"轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/135405.html
標籤:PHP
上一篇:Mac 下使用svn
