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-->
<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>

Golang正则表达式使用管道符|来匹配多个单词或字符串,将各个选项作为逻辑OR表达式分隔开来。例如:匹配"fox"或"dog":fox|dog匹配"quick"、"brown"或"lazy":(quick|brown|lazy)匹配"Go"、"Python"或"Java":Go|Python|Java匹配单词或4位邮政编码:([a-zA

PHP正则表达式是一种针对文本处理和转换的有力工具。它可以通过解析文本内容,并按照特定的模式进行替换或截取,达到有效管理文本信息的目的。其中,正则表达式的一个常见应用是替换以特定字符开头的字符串,对此,我们进行如下的讲解

js是弱类型语言,不能像C#那样使用param关键字来声明形参是一个可变参数。那么js中,如何实现这种可变参数呢?下面本篇文章就来聊聊JavaScript函数可变参数的实现方法,希望对大家有所帮助!

php用正则去除中文的方法:1、创建一个php示例文件;2、定义一个含有中文和英文的字符串;3、通过“preg_replace('/([\x80-\xff]*)/i','',$a);”正则方法去除查询结果中的中文字符即可。

在本文中,我们将学习如何使用PHP正则表达式删除HTML标签,并从HTML字符串中提取纯文本内容。 为了演示如何去掉HTML标记,让我们首先定义一个包含HTML标签的字符串。

好嘞,今天我们继续剖析下Python里的类。[[441842]]先前我们定义类的时候,使用到了构造函数,在Python里的构造函数书写比较特殊,他是一个特殊的函数__init__,其实在类里,除了构造函数还有很多其他格式为__XXX__的函数,另外也有一些__xx__的属性。下面我们一一说下:构造函数Python里所有类的构造函数都是__init__,其中根据我们的需求,构造函数又分为有参构造函数和无惨构造函数。如果当前没有定义构造函数,那么系统会自动生成一个无参空的构造函数。例如:在有继承关系

形参变量在未出现函数调用时并不占用内存,只在调用时才占用,调用结束后将释放内存。形参全称“形式参数”,是函数定义时使用的参数;但函数定义时参数是没有任实际何数据的,因而在函数被调用前没有为形参分配内存,其作用是说明自变量的类型和形态以及在过程中的作用。

Python作为一种高级编程语言,易于学习和使用。一旦需要编写Python程序时,无法避免地遇到语法错误,表达式语法错误是常见的一种。在本文中,我们将讨论如何解决Python的表达式语法错误。表达式语法错误是Python中最常见的错误之一,它通常是由于错误的使用语法或缺少必要组件而导致的。在Python中,表达式通常由数字、字符串、变量和运算符组成。最常见的


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

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