PHP正則表達式核心技術徹底詳解 第3節

做者:極客小俊 一個專一於web技術的80後
我不用拼過聰明人,我只須要拼過那些懶人 我就必定會超越大部分人!
CSDN@極客小俊,原創文章, B站技術分享
B站視頻 : 👉 Bilibili.com 👈
我的博客: 👉 cnblogs.com 👈php

在這裏插入圖片描述

咱們接着上一節的內容繼續說正則..web

模式修正符 或者叫 模式單元
語法: /原子+元字符+量詞/模式修正符號 或者說 /正則表達式/模式修正符
在php中模式修正符也叫修飾符,包含:i、m、s、U、e 模式修正符必須配合正則函數一塊兒使用纔有意義
例如: "/<img\ssrc=".?"/>/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 : 修正正則的貪婪模式
緣由: 正則表達式的特色:就是比較」貪婪「 .* .+ 全部字符都符合這個貪婪條件
修正貪婪以下方式:函數

  1. 使用模式修正符號 U
  2. 一種是使用?完成 .? .+?
    注意: 若是兩種方式同時出現又會開啓了貪婪模式 例如都存在 .
    ? /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位 以下:this

$string='wang12345';
$pattern='/^[a-zA-Z0-9]{1}[a-zA-Z0-9]{4,7}/';

preg_match($pattern, $string,$arr);
show($arr);

練習2:匹配Email 以下:url

$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);

練習3:匹配一個HTTP URL的正則表達式 以下:spa

/*
 * 匹配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:匹配手機號碼與座機電話號碼正則表達式 以下:.net

$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 :匹配時光網的首頁中的全部圖片爬取出來 以下:ssr

$string=file_get_contents('http://www.mtime.com/');
$pattern='/<img\ssrc=\".*?\"\/>/';
preg_match_all($pattern, $string,$arrList);

$patternReplace='/(?<=data-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>';
    }
}

練習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>';

在這裏插入圖片描述

"點贊" "評論" "收藏"

你們的支持就是我堅持下去的動力!

若是以上內容有任何錯誤或者不許確的地方,歡迎在下面 👇 留個言指出、或者你有更好的想法,歡迎一塊兒交流學習