1.2 正则表达式
术语解释
正则表达式是一种描述字符串结构的语法规则,是一个特定的格式化模式,可以匹配、替换、截取匹配的字符串。
grep:最初是 ED 编辑器中的一条命令,用来显示文件中特定的内容,后来成为一个独立的工具 grep。
egrep:贝尔实验室推出了egrep,意为“扩展的 grep”,这大大增强了正则表达式的能力。
POSIX(Portable Operating System Interface of Vnix)
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems.
Perl(Practical Extraction and Reporting Language)
Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier.
PCRE
Perl Compatible Regular Expressions (PCRE) is a library written in C, which implements a regular expression engine, inspired by the capabilities of the Perl programming language. Philip Hazel started writing PCRE in summer 1997.[3] PCRE's syntax is much more powerful and flexible than either of the POSIX regular expression flavors and than that of many other regular-expression libraries.
A number of prominent open-source programs, such as the Apache and Nginx HTTP servers, and the PHP and R scripting languages, incorporate the PCRE library; proprietary software can do likewise, as the library is BSD-licensed.
语法规则
一个完整的正则表达式由两部分构成,元字符和文本字符。元字符就是具有特殊含义的字符,如:“*”,“?”。文本字符就是普通的文本,如字母和数字等。
PCRE 风格的正则表达式一般都放置在定界符“/”中间。
行定位符(^和$)
"^"表示行的开始;
"$"表示行的结尾;
单词定界符(\b、\B)
"\b"表示要查找的字串为一个完整的单词;
"\B"表示一个要查找的字串不是一个完整的单词;
字符类([])
只要匹配的字符出现在方括号内,即可表示匹配成功。
POSIX 和 PCRE 都使用了一些预定义字符类。POSIX 风格的预定义字符类举例:
[:digit:]十进制数字集合。等同于[0-9]
[[:alnum:]]字母和数字的集合。等同于[a-zA-Z]
[[:alpha:]]字母集合。等同于[a-zA-Z] ...
而 PCRE 的预定义字符类则使用反斜线来表示。
连字符(-)
连字符可以表示字符的范围。
如:[a-zA-Z]
排除字符([^])
"^"在方括号中,表示排除的意思
限定符(? * + {n,m})
对于重复出现的字符或字串,可以使用限定符来实现匹配。
?:匹配前面的字符零次或一次
+:匹配前面的字符一次或多次
*:匹配前面的字符零次或多次
{n}:匹配前面的字符n次
{n,}:匹配前面的字符至少n次
{n,m}:匹配前面的字符最少n次,最多m次
点好字符(.)
可以匹配出换行符外的任意一个字符。
转义字符()
将特殊字符(如:“,”、“?”、“、”等)变为普通字符。
反斜线()
除了做转义字符外,反斜线还有一些其他功能。
反斜线可以将一些不可打印的字符显示出来;
可以预定义字符集
可以定义断言,如:“\b”、“\B”
括号字符(())
小括号字符的第一个作用就是可以改变限定字符的作用范围,如“|”、“*”、“^”等;
小括号字符的第二个作用是分组,也就是子表达式。如:(.[0-9]{1,3}){3}
反向引用
...
模式修饰符
...
PHP 中的 PCRE — 正则表达式(兼容 Perl)
preg_filter — 执行一个正则表达式搜索和替换
preg_grep — 返回匹配模式的数组条目
preg_last_error — 返回最后一个PCRE正则执行产生的错误代码
preg_match_all — 执行一个全局正则表达式匹配
preg_match — 执行匹配正则表达式
preg_quote — 转义正则表达式字符
preg_replace_callback_array — Perform a regular expression search and - replace using callbacks
preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换
preg_replace — 执行一个正则表达式的搜索和替换
preg_split — 通过一个正则表达式分隔字符串
Last updated