1. 正则表达式,就是一个匹配的“模式”
  2. 正则表达式本身,就是一个字符串(中有一些语法规则,特殊字符)
  3. 正则表达式这个字符串,一定要在对应的函数中使用,(分割的函数,替换的函数)
  4. PHP中主要使用 pcre 函数来处理正则,另一套posix 是 php4 版本中使用的比较多

什么是正则表达式

组成

  1. 定界符:多种都可以{}、|,常用//
  2. 原子: 最小的一个匹配单位,放在定界符中,在一个正则表达式中,至少要有一个原子
  3. 元字符:不能单独使用,修饰原子,是用来扩展原子功能的和限定功能的
  4. 模式修正符:修正,对模式(正则)修正(写在定界符号外面,写在右边)

定界符

在程序语言中,使用与 Perl 兼容的正则表达式,通常都需要将模式表达式放入定界符之间。如 “/”

定义

作为定界符常用 “/“ ,除了斜线”” ,其他的除了字母,数字之外的所有字符都可以作为定界符,如:#@!{}、|

原子

打印字符(a-z、A-Z、!@#$%^&()_+和非打印字符(回车等)

  1. 非打印字符
    • \cx 匹配由x指明的控制字符。如\cM匹配一个Ctrl-M或回车符。x的值必须为 Az之一
    • \f 匹配一个换页符。等价于:\x0c \cL
    • \n 匹配一个换行符。 \x0a \cJ
    • \r 匹配一个回车符。 \x0d \cM
    • \t 匹配一个制表符。 \x09 \cI
    • \v 匹配一个垂直制表符。 \x0b \cK
  1. //打印字符
  2. <?php
  3. $str = 'asdfsdfsdf3343sdfsadf34455BSASDFasdfddd<br>';
  4. $reg = "/a/";
  5. if(preg_match($reg, $str, $arr)){
  6. echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
  7. print_r($arr);
  8. }else{
  9. echo "没有匹配上<br/>";
  10. }
  11. 输出:
  12. 正则表达式 /a/ 和字符串 asdfsdfsdf3343sdfsadf34455BSASDFasdfddd
  13. 匹配成功!
  14. Array ( [0] => a )
  15. // 非打印字符 比如 回车
  16. <?php
  17. $str = 'asdfsdfsdf3343sdfs
  18. adf34455BSASDFasdfddd<br>';
  19. $reg = "/\n/";
  20. if(preg_match($reg, $str, $arr)){
  21. echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
  22. print_r($arr);
  23. }else{
  24. echo "没有匹配上<br/>";
  25. }
  26. 输出:
  27. 正则表达式 / / 和字符串 asdfsdfsdf3343sdfs adf34455BSASDFasdfddd
  28. 匹配成功!
  29. Array ( [0] => )

\ 转义字符,一些特殊字符加上\作为原子

  • 可以将有意义的字符转成没有意义的原子字符
  • 可以将没有意义的字符转成有意义的原子
  • a-z A-Z 0-9 _ 所有没有意义的字符,加上转义也没有意义的,都是可加可不加
// 将有意义的转成没有意义的院子字符,比如:.匹配所有的字符
<?php
    $str = 'asdfsd.fsdf3343sdfs
    adf34455BSASDFasdfddd<br>';

    $reg = "/\./";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "没有匹配上<br/>";
    }
输出:
正则表达式 /\./ 和字符串 asdfsd.fsdf3343sdfs adf34455BSASDFasdfddd
匹配成功!
Array ( [0] => . )

// 将没意义的转成有意义的原子字符,比如:\d 代表所有数字
<?php
    $str = 'asdfsd.fsdf3343sdfs
    adf34455BSASDFasdfddd<br>';

    $reg = "/\d/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "没有匹配上<br/>";
    }
输出:
正则表达式 /\d/ 和字符串 asdfsd.fsdf3343sdfs adf34455BSASDFasdfddd
匹配成功!
Array ( [0] => 3 )

// 没有特殊意义的字符,转义之后还是没有意义
<?php
    $str = 'asdfsd.fsd@f3343sdfs
    adf34455BSASDFasdfddd<br>';

    $reg = "/\@/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "没有匹配上<br/>";
    }
输出:
正则表达式 /\@/ 和字符串 asdfsd.fsd@f3343sdfs adf34455BSASDFasdfddd
匹配成功!
Array ( [0] => @ )

所有的数字,所有的字,所有的空白,所有的字母,特殊符号

\d : 任意一个数字 [0-9]

\D : 任意一个非数字 [^0-9]

\s : 空白 [\t\n\f\v]

\S : 非空白 [^\t\n\f\v]

\w : 代表任意一个字 a A 0 [a-zA-z]

\W : 任意一个非字 [^a-zA-z_]

自定义原子表

  1. [13579] : 原子表,原子表中的原子只有一个生效,只匹配一个
  2. [^a-zA-Z]
    • -,从哪到哪是一个区间范围比如从a到z;
    • ^,除了列表中的,取反
  3. .: 代表任意一个元素

元字符

  1. 元字符,不能在正则表达式中单独使用,用来修饰原子的

* 等价于 {0,}

匹配0次,1次或多次其前的原子(任意次)

实例

<?php
    $str = 'this google is a test';

    $reg = "/go*gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /go*gle/ 和字符串 this google is a test 匹配成功!
Array ( [0] => google )

+ 等价于 {1,}

匹配1次或多次其前的原子,不能没有至少要出现1次

实例

// 不能匹配0次
<?php
    $str = 'this ggle is a test';

    $reg = "/go+gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 匹配多次
<?php
    $str = 'this google is a test';

    $reg = "/go+gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /go+gle/ 和字符串 this google is a test 匹配成功!
Array ( [0] => google )

? 等价于 {0,1}

匹配0次或1次其前的原子

实例

// 不能匹配多次
<?php
    $str = 'this google is a test';

    $reg = "/go?gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 只能匹配0次或一次
<?php
    $str = 'this gogle is a test';

    $reg = "/go?gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }


输出:
正则表达式 /go?gle/ 和字符串 this gogle is a test 匹配成功!
Array ( [0] => gogle )

{n}

表示其前面的原子恰好出现n次,多一次和少一次都不可以

实例

// 不能匹配不是n的次数
<?php
    $str = 'this goooogle is a test';

    $reg = "/go{3}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 刚好匹配三次
<?php
    $str = 'this gooogle is a test';

    $reg = "/go{3}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /go{3}gle/ 和字符串 this gooogle is a test 匹配成功!
Array ( [0] => gooogle )

{n,}

表示其前面的原子出现不少于n次

实例

// 不能匹配少于n的次数
<?php
    $str = 'this google is a test';

    $reg = "/go{3,}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 刚好匹配n次
<?php
    $str = 'this gooogle is a test';

    $reg = "/go{3,}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /go{3,}gle/ 和字符串 this gooogle is a test 匹配成功!
Array ( [0] => gooogle )

// 匹配n次以上
<?php
    $str = 'this gooooogle is a test';

    $reg = "/go{3,}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /go{3,}gle/ 和字符串 this gooooogle is a test 匹配成功!
Array ( [0] => gooooogle )

{n,m}

表示其前面的原子至少出现n次,最多出现m次

实例

// 不能匹配少于n的次数
<?php
    $str = 'this google is a test';

    $reg = "/go{3,6}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 不能匹配大于n次的次数
<?php
    $str = 'this gooooooogle is a test';

    $reg = "/go{3,5}gle/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

|

匹配两个或多个分支选择,表示它两边的原子,只要有一个出现就可以,但是 | 优先级是最低的

实例

// 匹配其中一个
<?php
    $str = 'this  is a oracle';

    $reg = "/oracle|mysql/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /oracle|mysql/ 和字符串 this is a oracle 匹配成功!
Array ( [0] => oracle )

^或\A

匹配输入字符串的开始位置(或在多行模式下行的开头,即紧随一个换行符之后),这个必须写在正则表达式的最前面

实例

// 字符串必须以^后面的原子来开始
<?php
    $str = 'this is a oracle';

    $reg = "/^abc/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

<?php
    $str = 'abcthis is a oracle';

    $reg = "/^abc/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /^abc/ 和字符串 abcthis is a oracle 匹配成功!
Array ( [0] => abc )

$或\Z

匹配输入字符串的结束位置(或在多行模式下行的结尾,即紧随一个换行符之前),必须写在正则表达式的最后面

实例

// 字符串必须以$前面的原子来结束
<?php
    $str = 'this is a oracle';

    $reg = "/abc$/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

<?php
    $str = 'this is a oracleabc';

    $reg = "/abc$/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /abc$/ 和字符串 this is a oracleabc 匹配成功!
Array ( [0] => abc )

// 以abc开始,以abc结束
<?php
    $str = 'abcthis is a oracleabc';

    $reg = "/^abc.*abc$/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /^abc.*abc$/ 和字符串 abcthis is a oracleabc 匹配成功!
Array ( [0] => abcthis is a oracleabc )

\b

匹配单词的边界

实例

// 匹配单词
<?php
    $str = 'this is a isorcle';

    $reg = "/\bis\b/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
            print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /\bis\b/ 和字符串 this is a isorcle 匹配成功!
Array ( [0] => is )

\B

匹配除单词边界以外的部分

()

匹配其整体为一个原子,即模式单元,可以理解为由多个单个原子组成的大原子

  1. 必变优先级别
  2. 将小原子变成大原子,(mysql)*,可以匹配括号里的所有字符了
<?php
    $str = 'this isis a isorcle';

    $reg = "/(is)*/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

正则表达式 /(is)*/ 和字符串 this isis a isorcle 匹配成功!
Array ( [0] => )
  1. 子模式,整个表达式是一个大的模式,小括号中的是每个独立的子模式,可以把括号里的内容单独匹配出来
// 匹配字符串
<?php
    $str = 'this isis http://www.animallook.cn  a isorcle';

    $reg = "/http\:\/\/www\..*\.cn/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /http\:\/\/www\..*\.cn/ 和字符串 this isis http://www.animallook.cn a isorcle 匹配成功!
Array ( [0] => http://www.animallook.cn )

// 匹配子模式
<?php
    $str = 'this isis http://www.animallook.cn  a isorcle';

    $reg = "/(http|ftp|https)\:\/\/(www|mail|ftp)\.(.*)?\.(cn|com|net|org)/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /(http|ftp|https)\:\/\/(www|mail|ftp)\.(.*)?\.(cn|com|net|org)/ 和字符串 this isis http://www.animallook.cn a isorcle 匹配成功!
Array ( [0] => http://www.animallook.cn [1] => http [2] => www [3] => animallook [4] => cn )
  1. 反向引用
    • 反向引用可以使用 \n,来选择使用第几个子模式来匹配
    • 不需要 () 有那么多的作用,只希望它作为改变优先级和将小原子变成大原子使用的话,在括号中加入 ?: 就可以关闭括号子模式的功能
// 比如取出日期的时候需要匹配日期分隔符一致,就可以使用反向引用,引用子模式的匹配,

<?php
    $str = 'this isis 2019-07-12 http://www.animallook.com.cn  a isorcle';

    // \\1 就是引用第一个子模式
    $reg = "/\d{4}(-|\/)\d{2}\\1\d{2}/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /\d{4}(-|\/)\d{2}\1\d{2}/ 和字符串 this isis 2019-07-12 http://www.animallook.com.cn a isorcle 匹配成功!
Array ( [0] => 2019-07-12 [1] => - )

// 关闭子模式
<?php
    $str = 'this isis 2019-07-12 http://www.animallook.com.cn  a isorcle';

    $reg = "/(?:\d{4})(-|\/)\d{2}\\1\d{2}/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /(?:\d{4})(-|\/)\d{2}\1\d{2}/ 和字符串 this isis 2019-07-12 http://www.animallook.com.cn a isorcle 匹配成功!
Array ( [0] => 2019-07-12 [1] => - )

模式修正符(单个字符)

  1. 模式修正符号要写在定界符号外,放在右边,”/go*gle/i”
  2. 模式修正符,一个字符就是一个功能,可以组合使用

作用:模式修正符可以修正正则表达的解释,或扩充了正则表达式的功能

i : 修正正则表达式不区分大小写(默认是区分大小写的)

<?php
    $str = 'This isis 2019-07-12 http://www.animallook.com.cn  a isorcle';

    $reg = "/this/i";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /this/i 和字符串 This isis 2019-07-12 http://www.animallook.com.cn a isorcle 匹配成功!
Array ( [0] => This )

m : 修正正则表达式可以视为多行,在使用 ^或$ 这两个符号时,每一行满足就可以

// 视为一行的情况
<?php
    $str = 'This 
isis 2019-07-12 http://www.animallook.com.cn  a isorcle';

    $reg = "/^is/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 视为多行的情况
<?php
    $str = 'This 
isis 2019-07-12 http://www.animallook.com.cn  a isorcle';

    $reg = "/^is/m";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /^is/m 和字符串 This isis 2019-07-12 http://www.animallook.com.cn a isorcle 匹配成功!
Array ( [0] => is )

s : 修正正则表达式中的 . 可以匹配换行符号(默认 . 不能匹配回车符号)

// 默认 . 不能匹配回车符号
<?php
    $str = 'This is a te
st';

    $reg = "/te.*st/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 修正后就可以匹配
<?php
    $str = 'This is a te
st';

    $reg = "/te.*st/s";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /te.*st/s 和字符串 This is a te st 匹配成功!
Array ( [0] => te st )

x : 修正正则表达式,可以省略空白

// 不省略空白
<?php
    $str = 'This is a test webserver';

    $reg = "/web server/";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

// 省略空白
<?php
    $str = 'This is a test webserver';

    $reg = "/web server/x";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /web server/x 和字符串 This is a test webserver 匹配成功!
Array ( [0] => webserver )

// 但有空白的就匹配不到了
<?php
    $str = 'This is a test web server';

    $reg = "/web server/x";

    if(preg_match($reg, $str, $arr)){
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
    }else{
        echo "匹配失败!<br/>";
    }

输出:
匹配失败!

e :

U : (.),(.+),匹配比较贪婪,U就是取消贪婪模式,但不建议使用,建议:.? , .+?

正则表达式的编写

  1. 正则表达式就是一种语言,要把开发思想放进去
  2. 列需求

编写url匹配正则

<?php
$str = "
    这是http://www.lampbrother.net网站
    这是https://www.animallook.net/php/网站
    这是https://www.animallook.net/php/demo.php网站
    这是http://www.animallook.cn/php/demo.inc.php?username=admin&p=123456网站
    这是http://www.baidu.com网站
    这是http://www.aaa.baidu.com网站
    这是ftp://mail.baidu.org网站
    这是ftps://www.anima.top网站
";
    // % 是因为有些url中文字符会转义为 %加数字
    $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn|top)([\w-\.\/\=\?\&\%]*)?/';

    if(preg_match_all($reg, $str, $arr)){
        echo "
<pre>";
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
        echo "</pre>";
    }else{
        echo "匹配失败!<br/>";
    }

输出:
正则表达式 /(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn|top)([\w-\.\/\=\?\&\%]*)?/ 和字符串 
    这是http://www.lampbrother.net网站
    这是https://www.animallook.net/php/网站
    这是https://www.animallook.net/php/demo.php网站
    这是http://www.animallook.cn/php/demo.inc.php?username=admin&p=123456网站
    这是http://www.baidu.com网站
    这是http://www.aaa.baidu.com网站
    这是ftp://mail.baidu.org网站
    这是ftps://www.anima.top网站
 匹配成功!
Array
(
    [0] => Array
        (
            [0] => http://www.lampbrother.net
            [1] => https://www.animallook.net/php/
            [2] => https://www.animallook.net/php/demo.php
            [3] => http://www.animallook.cn/php/demo.inc.php?username=admin&p=123456
            [4] => http://www.baidu.com
            [5] => http://www.aaa.baidu.com
            [6] => ftp://mail.baidu.org
            [7] => ftps://www.anima.top
        )

    [1] => Array
        (
            [0] => http
            [1] => https
            [2] => https
            [3] => http
            [4] => http
            [5] => http
            [6] => ftp
            [7] => ftps
        )

    [2] => Array
        (
            [0] => www
            [1] => www
            [2] => www
            [3] => www
            [4] => www
            [5] => www
            [6] => mail
            [7] => www
        )

    [3] => Array
        (
            [0] => lampbrother
            [1] => animallook
            [2] => animallook
            [3] => animallook
            [4] => baidu
            [5] => aaa.baidu
            [6] => baidu
            [7] => anima
        )

    [4] => Array
        (
            [0] => net
            [1] => net
            [2] => net
            [3] => cn
            [4] => com
            [5] => com
            [6] => org
            [7] => top
        )

    [5] => Array
        (
            [0] => 
            [1] => /php/
            [2] => /php/demo.php
            [3] => /php/demo.inc.php?username=admin&p=123456
            [4] => 
            [5] => 
            [6] => 
            [7] => 
        )

)

编写email正则表达式

正则匹配查找

preg_match() 分割

  1. 匹配字符串,能匹配到返回1,不能匹配到返回0
<?php
$str = "
    这是http://www.lampbrother.net网站
    这是https://www.animallook.net/php/网站
    这是https://www.animallook.net/php/demo.php网站
    这是http://www.animallook.cn/php/demo.inc.php?username=admin&p=123456网站
    这是http://www.baidu.com网站
    这是http://www.aaa.baidu.com网站
    这是ftp://mail.baidu.org网站
    这是ftps://www.anima.top网站
";
    // % 是因为有些url中文字符会转义为 %加数字
    $reg = '/(https?|ftps?)\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn|top)([\w-\.\/\=\?\&\%]*)?/';

    if($aaa = preg_match($reg, $str, $arr)){
        echo "
<pre>";
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
        echo "</pre>";
    }else{
        echo "匹配失败!<br/>";
    }
var_dump($aaa);

输出:
匹配失败!
int(0)

<?php
$str = "
    这是http://www.lampbrother.net网站
    这是https://www.animallook.net/php/网站
    这是https://www.animallook.net/php/demo.php网站
    这是http://www.animallook.cn/php/demo.inc.php?username=admin&p=123456网站
    这是http://www.baidu.com网站
    这是http://www.aaa.baidu.com网站
    这是ftp://mail.baidu.org网站
    这是ftps://www.anima.top网站
";
    // % 是因为有些url中文字符会转义为 %加数字
    $reg = '/(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn|top)([\w-\.\/\=\?\&\%]*)?/';

    if($aaa = preg_match($reg, $str, $arr)){
        echo "
<pre>";
        echo "正则表达式 <b>{$reg}</b> 和字符串 <b>{$str}</b> 匹配成功!<br>";
        print_r($arr);
        echo "</pre>";
    }else{
        echo "匹配失败!<br/>";
    }
var_dump($aaa);

输出:
正则表达式 /(https?|ftps?):\/\/(www|mail|bbs|ftp)\.(.*?)\.(net|com|org|cn|top)([\w-\.\/\=\?\&\%]*)?/ 和字符串 
    这是http://www.lampbrother.net网站
    这是https://www.animallook.net/php/网站
    这是https://www.animallook.net/php/demo.php网站
    这是http://www.animallook.cn/php/demo.inc.php?username=admin&p=123456网站
    这是http://www.baidu.com网站
    这是http://www.aaa.baidu.com网站
    这是ftp://mail.baidu.org网站
    这是ftps://www.anima.top网站
 匹配成功!
Array
(
    [0] => http://www.lampbrother.net
    [1] => http
    [2] => www
    [3] => lampbrother
    [4] => net
    [5] => 
)
int(1)

preg_match_all()

preg_grep()