Regular expressions and PCRE function in PHP_PHP tutorial
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>
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-->用于查找任何前面不是 ”foo” 的 ”bar”
<code>模式修饰符
<code>模式修饰符 |
|
---|---|
<code>U |
<code>这个修饰符逆转了量词的”贪婪”模式,使量词默认为非贪婪的 |
<code>i |
<code>大小写不敏感匹配 |
<code>x |
<code>忽略空白 |
<code>s |
<code>点号元字符匹配所有字符,包含换行符。如果没有这个修饰符,点号不匹配换行符 |
<code>… |
<code>PCRE 函数
<code><code><code>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 — 通过一个正则表达式分隔字符串 </code></code></code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.
