Home > Backend Development > PHP Tutorial > Regular expressions and PCRE function in PHP_PHP tutorial

Regular expressions and PCRE function in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:44:06
Original
950 people have browsed it

Regular expressions and PCRE functions in PHP

PCRE

PHP has two different ways to use regular expressions: PCRE (Perl compatible notation, preg_*) functions and POSIX (POSIX extended notation, ereg_*) functions. Fortunately, the POSIX family of functions has been deprecated starting with PHP 5.3.0.


Regular expression

Delimiter

Commonly used delimiters are forward slash (/), hash symbol (#) and negation symbol (~). The following examples all use legal delimiter patterns

<code>/foo bar/
#^[^0-9]$#
+php+
%[a-zA-Z0-9_-]%
{this is a pattern}
</code>
Copy after login

You can add pattern modifiers after the end delimiter

Metacharacters

Some characters are given special meanings so that they no longer simply represent themselves. This type of coded characters with special meanings in the pattern is called 元字符.

元字符 描述
一般用于转义字符
^ 断言目标的开始位置(或在多行模式下是行首)
$ 断言目标的结束位置(或在多行模式下是行尾)
. 匹配除换行符外的任何字符(默认)
[ 开始字符类定义
] 结束字符类定义
| 开始一个可选分支
( 子组的开始标记
) 子组的结束标记
? 作为量词,表示 0 次或 1 次匹配。位于量词后面用于改变量词的贪婪特性。 (查阅量词)
* 量词,0 次或多次匹配
量词,1 次或多次匹配
{ 自定义量词开始标记
} 自定义量词结束标记


The portion of the pattern enclosed in square brackets is called the "character class". Only the following metacharacters are available within a character class

元字符 描述
转义字符
^ 仅在作为第一个字符(方括号内)时,表明字符类取反
- 标记字符范围

Character class

The content in square brackets is the character class

There are some predefined character classes

字符类 描述
d 任意十进制数字
D 任意非十进制数字
h 任意水平空白字符(since PHP 5.2.4)
H 任意非水平空白字符(since PHP 5.2.4)
s 任意空白字符
S 任意非空白字符
任意垂直空白字符(since PHP 5.2.4)
V 任意非垂直空白字符(since PHP 5.2.4)
w 任意单词字符
W 任意非单词字符

Atomic

Visible atoms

asabc

Invisible atoms

as

Quantifier

量词
* 等价于 {0,}
等价于 {1,}
? 等价于 {0,1}

断言

简单的断言代码有、B、 A、 Z、z、 ^、$

前瞻断言

从当前位置向前测试

(?=) (?!)

w+(?=;)匹配一个单词紧跟着一个分号但是匹配结果不会包含分号

后瞻断言

从当前位置向后测试

(?<=) (?<!--)</code-->

<code>(?<!--foo)bar</code-->用于查找任何前面不是 &rdquo;foo&rdquo; 的 &rdquo;bar&rdquo;

<code>模式修饰符

<code>模式修饰符
<code>U <code>这个修饰符逆转了量词的&rdquo;贪婪&rdquo;模式,使量词默认为非贪婪的
<code>i <code>大小写不敏感匹配
<code>x <code>忽略空白
<code>s <code>点号元字符匹配所有字符,包含换行符。如果没有这个修饰符,点号不匹配换行符
<code>&hellip;

<code>PCRE 函数

<code><code><code>preg_filter &mdash; 执行一个正则表达式搜索和替换

preg_grep &mdash; 返回匹配模式的数组条目

preg_last_error &mdash; 返回最后一个PCRE正则执行产生的错误代码

preg_match_all &mdash; 执行一个全局正则表达式匹配

preg_match &mdash; 执行一个正则表达式匹配

preg_quote &mdash; 转义正则表达式字符

preg_replace_callback_array &mdash; Perform a regular expression search and replace using callbacks

preg_replace_callback &mdash; 执行一个正则表达式搜索并且使用一个回调进行替换

preg_replace &mdash; 执行一个正则表达式的搜索和替换

preg_split &mdash; 通过一个正则表达式分隔字符串
</code></code></code>
Copy after login

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1050841.htmlTechArticlePHP中的正则表达式及PCRE函数 PCRE PHP有两种使用不同的方式来使用正则表达式:PCRE(Perl兼容表示法,preg_*)函数 和 POSIX(POSIX 扩展表示法,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template