一,认识正则
首先看下面的代码 ,
$str声明一个字符串 ,
$patt 匹配规则,
preg_match_all()函数用于执行一个全局正则表达式匹配。
<?php
$str = 'hi,this is his history';
$patt = '/hi/';
preg_match_all($patt,$str,$matches);
print_r($matches);
匹配结果
正则,我们平时用的不多,所以容易忘;
所以在记忆时着重点进行记忆,找谁?怎么找?找几个?
二,常用字符簇
三,单词匹配
<?php
$str = 'hi,this is history111**+ ';
$patt = '/\Bhi\B/';
preg_match_all($patt,$str,$matches);
print_r($matches);
$str = 'hi,this is history111**+ ';
$patt = '/\bhi\b/';
preg_match_all($patt,$str,$matches);
print_r($matches);
四,集合与补集
<?php
//集合示例代码
$str = ['13800138000','13426060134','170235','18265432185932545'];
//寻找包含0123456 同时又是11位的元素
$patt = '/^[0123456]{11}$/';
foreach ($str as $k => $v) {
preg_match_all($patt,$v,$matches);
print_r($matches);
}
echo '<hr>';
//补集示例代码
$str = ['13800138000','13426060134','170235','18265432185932545'];
//寻找不包含7 同时又是11位的元素
$patt = '/^[^7]{11}$/';
foreach ($str as $k => $v) {
preg_match_all($patt,$v,$matches);
print_r($matches);
}
来源:https://blog.csdn.net/weixin_43162776/article/details/102734698