正则匹配的修正模式

常用的修正模式
U 懒惰匹配
i 忽略英文字符大小写
x 忽略正则表达式的空白
s 让元字符‘.’匹配包括换行在内的所有字符
*e
u utf8格式的匹配模式字符串必须要使用
例子:
懒惰模式:

$subject = 'woqu imooc_123123123123123';

$pattern1 = '/imooc.+123/';//贪婪模式
preg_match($pattern1,$subject,$arr1);
show($arr1);
/*
Array
(
    [0] => imooc_123123123123123
)
*/

如上例子,想要截取到 imooc_123(默认的贪婪模式),就得使用懒惰模式,正则表达式需要如下写法:

$pattern1 = '/imooc.+123/U';//懒惰模式 表达式尾处多了个 U
/*
Array
(
    [0] => imooc_123
)
*/

i忽略英文字符大小写

$pattern1 = '/iMooC.+123/i';//正则中出现大小写字母,加上i 正常可以别匹配
/*
Array
(
    [0] => imooc_123123123123123
)
*/

懒惰模式+忽略英文大小写:

$pattern1 = '/imooc.+123/Ui';//懒惰模式+忽略大小写
/*
Array
(
    [0] => imooc_123
)
*/

忽略正则表达式的空白

$pattern1 = '/iMo oc.+123/Uix';//懒惰模式+忽略大小写+忽略空白
/*
Array
(
    [0] => imooc_123
)
*/

猜你喜欢

转载自blog.csdn.net/qq_17040587/article/details/82251167