正则表达式学习


正则表达式学习


正文

正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。 Linux下支持正则表达式查询的库有PCRE、Boost等。

PCRE(Perl Compatible Regular Expressions 中文含义:perl语言兼容正则表达式)是一个用C语言编写的正则表达式函数库, 由菲利普.海泽(Philip Hazel)编写。PCRE是一个轻量级的函数库,比Boost之类的正则表达式库小得多。 PCRE十分易用,同时功能也很强大,性能超过了POSIX正则表达式库和一些经典的正则表达式库。 和Boost正则表达式库的比较显示,双方的性能相差无几,PCRE在匹配简单字符串时更快,Boost则在匹配较长字符串时胜出,但两者差距很小。 Nginx的rewrite,PHP的正则匹配(preg_match()、preg_match_all()等)等,都依赖于PCRE库。

正则表达式中的概念有:

  • 定界符
  • 模式表达式
  • 打印字符
  • 非打印字符
  • 原子、普通字符
  • 元字符
  • 自定义原子表
  • \ 转义
  • -范围
  • ^ 除了
  • 限定符
  • 定位符 ^ $ \b \B
  • 选择 ()
  • 反向引用 \n
  • 范围集合
  • 模式修正符
  • 贪婪匹配
  • 运算符优先级

PHP

<?php
$key = 'DELETE,POST <controller:\w+>/<id:\d+>';
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches);
print_r($matches);
// 输出
Array
(
    [0] => DELETE,POST <controller:\w+>/<id:\d+>
    [1] => DELETE,POST
    [2] => DELETE
    [3] => POST
    [4] => <controller:\w+>/<id:\d+>
)

$key = 'DELETE <controller:\w+>/<id:\d+>';
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches);
print_r($matches);
// 输出
Array
(
    [0] => DELETE <controller:\w+>/<id:\d+>
    [1] => DELETE
    [2] => 
    [3] => DELETE
    [4] => <controller:\w+>/<id:\d+>
)

$key = 'DELETE,POST,PUT,GET <controller:\w+>/<id:\d+>';
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches);
print_r($matches);
// 输出
Array
(
    [0] => DELETE,POST,PUT,GET <controller:\w+>/<id:\d+>
    [1] => DELETE,POST,PUT,GET
    [2] => PUT
    [3] => GET
    [4] => <controller:\w+>/<id:\d+>
)
<?php
$route = "<controller>/<action>";
preg_match_all('/<(\w+)>/', $route, $matches);
print_r($matches);
// 输出
Array
(
    [0] => Array
        (
            [0] => <controller>
            [1] => <action>
        )

    [1] => Array
        (
            [0] => controller
            [1] => action
        )

)

$route = "<controller>/delete";
preg_match_all('/<(\w+)>/', $route, $matches);
print_r($matches);
// 输出
Array
(
    [0] => Array
        (
            [0] => <controller>
        )

    [1] => Array
        (
            [0] => controller
        )

)

$route = "user/profile";
preg_match_all('/<(\w+)>/', $route, $matches);
print_r($matches);
// 输出
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)
<?php
$pattern = "/post/<action:\w+>/<id:\d+>/";
preg_match_all('/<(\w+):?([^>]+)?>/', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
print_r($matches);
// 输出
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => <action:\w+>
                    [1] => 6
                )

            [1] => Array
                (
                    [0] => action
                    [1] => 7
                )

            [2] => Array
                (
                    [0] => \w+
                    [1] => 14
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => <id:\d+>
                    [1] => 19
                )

            [1] => Array
                (
                    [0] => id
                    [1] => 20
                )

            [2] => Array
                (
                    [0] => \d+
                    [1] => 23
                )

        )

)

$pattern = "<controller>/delete";
preg_match_all('/<(\w+):?([^>]+)?>/', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
print_r($matches);
// 输出
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => <controller>
                    [1] => 0
                )

            [1] => Array
                (
                    [0] => controller
                    [1] => 1
                )

        )

)

$pattern = "user/profile";
preg_match_all('/<(\w+):?([^>]+)?>/', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
print_r($matches);
// 输出
Array
(
)






参考资料

正则表达式 - 教程 https://www.runoob.com/regexp/regexp-tutorial.html

PHP 正则表达式(PCRE) https://www.runoob.com/php/php-pcre.html

正则表达式修饰符(模式修正符) i、g、m、s、U、x、a、D、e 等 https://www.cnblogs.com/kinwing/p/11128796.html


返回