PHP正则表达式核心技术完全详解 第3节

作者:极客小俊 一个专注于web技术的80后
我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人!
CSDN@极客小俊,原创文章, B站技术分享
个人博客: cnblogs.com
前端html+css+javascript技术讨论交流群: 281499395
后端php+mysql+Linux技术交流群: 855256321

在这里插入图片描述

我们接着上一节的内容继续说正则..

模式修正符 或者叫 模式单元
语法: /原子+元字符+量词/模式修正符号 或者说 /正则表达式/模式修正符
在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位 如下:

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

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

练习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);

练习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=\".*?\"\/>/';
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>';


在这里插入图片描述

"点赞" "评论" "收藏"

大家的支持就是我坚持下去的动力!

如果以上内容有任何错误或者不准确的地方,欢迎在下面 👇 留个言指出、或者你有更好的想法,欢迎一起交流学习
关注 极客小俊 微信公众号 不定期更新免费技术干货


posted @ 2020-09-28 03:33  极客小俊  阅读(231)  评论(0编辑  收藏  举报