


正则表达式(Regular Expression,缩写为regexp,regex或regxp),又称正规表达式、正规表示式或常规表达式或正规化表示法或正规表示法,是指一个用 来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或替换那些符合某个模式的文本 内容。许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的在正则表达式引擎。正则表达式这个概念最初是由 Unix中的工具软件(例如sed和grep)普及开的。(摘自维基百科)
PHP同时使用两套正则表达式规则,一套是由电气和电子工程师 协会(IEEE)制定的POSIX Extended 1003.2兼容正则(事实上PHP对此标准的支持并不完善),另一套来自PCRE(Perl Compatible Regular Expression)库提供PERL兼容正则,这是个开放源代码的软件,作者为 Philip Hazel。
使用POSIX兼容规则的函数有:
ereg_replace()
ereg()
eregi()
eregi_replace()
split()
spliti()
sql_regcase()
mb_ereg_match()
mb_ereg_replace()
mb_ereg_search_getpos()
mb_ereg_search_getregs()
mb_ereg_search_init()
mb_ereg_search_pos()
mb_ereg_search_regs()
mb_ereg_search_setpos()
mb_ereg_search()
mb_ereg()
mb_eregi_replace()
mb_eregi()
mb_regex_encoding()
mb_regex_set_options()
mb_split()
使用PERL兼容规则的函数有:
preg_grep()
preg_replace_callback()
preg_match_all()
preg_match()
preg_quote()
preg_split()
preg_replace()
定界符:
POSIX兼容正则没有定界符,函数的相应参数会被认为是正则。
PERL兼容正则可以使用任何不是字母、数字或反斜线(\)的字符作为定界符,如果作为定界符的字符必须被用在表达式本身中,则需要用反斜线转义。也可以使用(),{},[] 和 作为定界符
修正符:
POSIX兼容正则没有修正符。
PERL兼容正则中可能使用的修正符(修正符中的空格和换行被忽略,其它字符会导致错误):
i (PCRE_CASELESS):
匹配时忽略大小写。
m(PCRE_MULTILINE):
当设定了此修正符,行起始(^)和行结束($)除了匹配整个字符串开头和结束外,还分别匹配其中的换行符(\n)的之后和之前。
s(PCRE_DOTALL):
如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符。
x(PCRE_EXTENDED):
如果设定了此修正符,模式中的空白字符除了被转义的或在字符类中的以外完全被忽略。
e:
如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,将其作为 PHP 代码求值,并用其结果来替换所搜索的字符串。 只有 preg_replace() 使用此修正符,其它 PCRE 函数将忽略之。
A(PCRE_ANCHORED):
如果设定了此修正符,模式被强制为“anchored”,即强制仅从目标字符串的开头开始匹配。
D(PCRE_DOLLAR_ENDONLY):
如果设定了此修正符,模式中的行结束($)仅匹配目标字符串的结尾。没有此选项时,如果最后一个字符是换行符的话,也会被匹配在里面。如果设定了 m 修正符则忽略此选项。
S:
当一个模式将被使用若干次时,为加速匹配起见值得先对其进行分析。如果设定了此修正符则会进行额外的分析。目前,分析一个模式仅对没有单一固定起始字符的 non-anchored 模式有用。
U(PCRE_UNGREEDY):
使“?”的默认匹配成为贪婪状态的。
X(PCRE_EXTRA):
模式中的任何反斜线后面跟上一个没有特殊意义的字母导致一个错误,从而保留此组合以备将来扩充。默认情况下,一个反斜线后面跟一个没有特殊意义的字母被当成该字母本身。
u(PCRE_UTF8):
模式字符串被当成UTF-8。
逻辑区隔:
POSIX兼容正则和PERL兼容正则的逻辑区隔符号作用和使用方法完全一致:
[]:包含任选一操作的相关信息。
{}:包含匹配次数的相关信息。
():包含一个逻辑区间的相关信息,可被用来进行引用操作。
|:表示“或”,[ab]和a|b是等价的。
metacharacter is related to "[]":
There are two different sets of metacharacters: one is recognized within the pattern except square brackets, and the other is recognized within square brackets "[]".
Posix-compatible regular and PERL-compatible regular "[]" and "consistent" metacharacters:
Universal escape character with several uses
^ Match the beginning of the string
$ Match the end of the string
? Match 0 or 1
* Match 0 or more characters of the previously specified type
+ Match 1 or more previously specified types The characters
POSIX-compatible regex and PERL-compatible regex "inconsistent" metacharacters "other than []":
. PERL-compatible regex matches any one except a newline character Character
. POSIX compatible regular match any character
POSIX compatible regular and PERL compatible regular "within []" "consistent" metacharacters:
Universal escape character with several uses
^ Negate the character, but only valid when it is the first character
- Specify the character ASCII range, study the ASCII code carefully, you will find that [W-c] is equivalent to [WXYZ\^_`abc]
POSIX-compatible regular expressions and PERL-compatible regular expressions "within []" are "inconsistent" metacharacters:
- The specification of [a-c-e] in POSIX-compatible regular expressions will throw An error occurred.
- The specification of [a-c-e] in PERL compatible regular expressions is equivalent to [a-e].
The number of matches is related to "{}":
POSIX compatible regular expressions and PERL compatible regular expressions are exactly the same in terms of matching times:
{2}: indicates matching The previous character 2 times
{2,}: means matching the previous character 2 or more times, the default is greedy (as many as possible) matching
{2,4}: means matching the previous character 2 times or 4 times
Logical intervals are related to "()":
The area enclosed by () is a logical interval. The main function of the logical interval is to reflect the occurrence of some characters. The logical order, another use is that it can be used for reference (the value in this range can be referenced to a variable). The latter function is rather strange:
$str = "http://www.163.com/";
// POSIX compatible regular:
echo ereg_replace("(. +)","\1",$str);
// PERL compatible regular:
echo preg_replace("/(.+)/", "$1",$str);
// Display two links
?>
When quoting, parentheses can be nested, and the logical order is calibrated according to the order in which "(" appears.
Type matching:
POSIX compatible regular:
[:upper:]: matches all uppercase letters
[: lower:]: Matches all lowercase letters
[:alpha:]: Matches all letters
[:alnum:]: Matches all letters and numbers
[:digit:]: Matches all numbers
[:xdigit:]: Matches all hexadecimal characters, equivalent to [0-9A-Fa-f]
[:punct:]: Matches all punctuation marks, equivalent to [. ,"'?!;:]
[:blank:]: matches spaces and TAB, equivalent to [ t]
[:space:]: matches all blank characters, equivalent to [ tnrfv]
[:cntrl:]: Matches all control characters between ASCII 0 and 31.
[:graph:]: Matches all printable characters, equivalent to: [^ tnrfv]
[:print :]: Matches all printable characters and spaces, equivalent to: [^tnrfv]
[.c.]: Unknown function
[=c=]: Unknown function
[:<: matches the beginning of a word> [:>:]: Matches the end of a word
PERL compatible regularity (here you can see the power of PERL regularity):
a alarm, that is, BEL character ('0)
cx "control-x ", where 0D)
t tab ('0)
xhh the character with hexadecimal code hh
ddd the character with octal code ddd, or backreference
d any decimal digit
D Any non-decimal character
s Any blank character
S Any non-blank character
w Any "word" character
W Any "non-word" character
b word boundary
B non-word boundary
A beginning of target (independent of multiline mode)
Z end of target or before the ending newline (independent of multiline mode)
z End of target (independent of multiline mode)
G First matching position in target

Python解析XML中的特殊字符和转义序列XML(eXtensibleMarkupLanguage)是一种常用的数据交换格式,用于在不同系统之间传输和存储数据。在处理XML文件时,经常会遇到包含特殊字符和转义序列的情况,这可能会导致解析错误或者误解数据。因此,在使用Python解析XML文件时,我们需要了解如何处理这些特殊字符和转义序列。一、特殊字符和

Python编程解析百度地图API文档中的坐标转换功能导读:随着互联网的快速发展,地图定位功能已经成为现代人生活中不可或缺的一部分。而百度地图作为国内最受欢迎的地图服务之一,提供了一系列的API供开发者使用。本文将通过Python编程,解析百度地图API文档中的坐标转换功能,并给出相应的代码示例。一、引言在开发中,我们有时会涉及到坐标的转换问题。百度地图AP

使用Python解析SOAP消息SOAP(SimpleObjectAccessProtocol)是一种基于XML的远程过程调用(RPC)协议,用于在网络上不同的应用程序之间进行通信。Python提供了许多库和工具来处理SOAP消息,其中最常用的是suds库。suds是Python的一个SOAP客户端库,可以用于解析和生成SOAP消息。它提供了一种简单而

随着PHP8.0的发布,许多新特性都被引入和更新了,其中包括XML解析库。PHP8.0中的XML解析库提供了更快的解析速度和更好的可读性,这对于PHP开发者来说是一个重要的提升。在本文中,我们将探讨PHP8.0中的XML解析库的新特性以及如何使用它。什么是XML解析库?XML解析库是一种软件库,用于解析和处理XML文档。XML是一种用于将数据存储为结构化文档

使用Python解析带有命名空间的XML文档XML是一种常用的数据交换格式,能够适应各种应用场景。在处理XML文档时,有时会遇到带有命名空间(namespace)的情况。命名空间可以防止不同XML文档中元素名的冲突,提高了XML的灵活性和可扩展性。本文将介绍如何使用Python解析带有命名空间的XML文档,并给出相应的代码示例。首先,我们需要导入xml.et

PHP中的HTTPBasic鉴权方法解析及应用HTTPBasic鉴权是一种简单但常用的身份验证方法,它通过在HTTP请求头中添加用户名和密码的Base64编码字符串进行身份验证。本文将介绍HTTPBasic鉴权的原理和使用方法,并提供PHP代码示例供读者参考。一、HTTPBasic鉴权原理HTTPBasic鉴权的原理非常简单,当客户端发送一个请求时

PHP爬虫是一种自动化获取网页信息的程序,它可以获取网页代码、抓取数据并存储到本地或数据库中。使用爬虫可以快速获取大量的数据,为后续的数据分析和处理提供巨大的帮助。本文将介绍如何使用PHP实现一个简单的爬虫,以获取网页源码和内容解析。一、获取网页源码在开始之前,我们应该先了解一下HTTP协议和HTML的基本结构。HTTP是HyperText

PHP中的单点登录(SSO)鉴权方法解析引言:随着互联网的发展,用户通常要同时访问多个网站进行各种操作。为了提高用户体验,单点登录(SingleSign-On,简称SSO)应运而生。本文将探讨PHP中的SSO鉴权方法,并提供相应的代码示例。一、什么是单点登录(SSO)?单点登录(SSO)是一种集中化认证的方法,在多个应用系统中,用户只需要登录一次,就能访问


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

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.

Dreamweaver Mac version
Visual web development tools