Table of Contents
Regular expressions and PCRE functions in PHP
PCRE
Regular expression
Delimiter
Metacharacters
Character class
Atomic
Visible atoms
Invisible atoms
Quantifier
断言
前瞻断言
后瞻断言
模式修饰符
PCRE 函数
Home Backend Development PHP Tutorial Regular expressions and PCRE function in PHP_PHP tutorial

Regular expressions and PCRE function in PHP_PHP tutorial

Jul 13, 2016 am 09:44 AM
function regular expression

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+(?=;)匹配一个单词紧跟着一个分号但是匹配结果不会包含分号

后瞻断言

从当前位置向后测试

(?&lt;=) (?<!--)</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 扩展表示法,...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

Considerations for parameter order in C++ function naming

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

How to match multiple words or strings using Golang regular expression?

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

How to write efficient and maintainable functions in Java?

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

Complete collection of excel function formulas

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

What are the benefits of C++ functions returning reference types?

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Advanced usage of reference parameters and pointer parameters in C++ functions

See all articles