Home php教程 php手册 php正则表达式的特殊字符含义

php正则表达式的特殊字符含义

Jun 13, 2016 am 10:46 AM
php about meaning character right article regular of expression

一篇关于正则表达式特殊字符含义的文章,希望对大家有所帮助。
 
字符/
意义:对于字符,通常表示按字面意义,指出接着的字符为特殊字符,不作解释。
例如:/b/匹配字符’b’,通过在b 前面加一个反斜杠,也就是/b/,则该字符变成特殊字符,表示
匹配一个单词的分界线。
或者:
对于几个字符,通常说明是特殊的,指出紧接着的字符不是特殊的,而应该按字面解释。
例如:*是一个特殊字符,匹配任意个字符(包括0个字符);例如:/a*/意味匹配0个或多个a。为了匹配字面上的*,在a前面加一个反斜杠;例如:/a*/匹配’a*’。
 
字符^
意义:表示匹配的字符必须在最前边。
例如:/^A/不匹配"an A,"中的’A’,但匹配"An A."中最前面的’A’。
 
字符$
意义:与^类似,匹配最末的字符。
例如:/t$/不匹配"eater"中的’t’,但匹配"eat"中的’t’。
 
字符*
意义:匹配*前面的字符0次或n次。
例如:/bo*/匹配"A ghost booooed"中的’boooo’或"A bird warbled"中的’b’,但不匹配"Agoat g
runted"中的任何字符。
 
字符+
意义:匹配+号前面的字符1次或n次。等价于{1,}。
例如:/a+/匹配"candy"中的’a’和"caaaaaaandy."中的所有’a’。
 
字符?
意义:匹配?前面的字符0次或1次。
例如:/e?le?/匹配"angel"中的’el’和"angle."中的’le’。
 
字符.
意义:(小数点)匹配除换行符外的所有单个的字符。
例如:/.n/匹配"nay, an apple is on the tree"中的’an’和’on’,但不匹配’nay’。
 
字符(x)
意义:匹配’x’并记录匹配的值。
例如:/(foo)/匹配和记录"foo bar."中的’foo’。匹配子串能被结果数组中的素[1], ...,[n] 返
回,或被RegExp对象的属性, ..., 返回。
 
字符x│y
意义:匹配’x’或者’y’。
例如:/green│red/匹配"green apple"中的’green’和"red apple."中的’red’。
 
字符{ n }
意义:这里的n是一个正整数。匹配前面的n个字符。
例如:/a{ 2 }/不匹配"candy,"中的’a’,但匹配"caandy," 中的所有’a’和"caaandy."中前面的两个’a’。
 
字符{ n, }
意义:这里的n是一个正整数。匹配至少n个前面的字符。
例如:/a{ 2, }不匹配"candy"中的’a’,但匹配"caandy"中的所有’a’和"caaaaaaandy."中的所有’a’
 
字符{ n,m }
意义:这里的n和m都是正整数。匹配至少n个最多m个前面的字符。
例如:/a{ 1,3 }/不匹配"cndy"中的任何字符,但匹配"candy,"中的’a’,"caandy," 中的前面两个
’a’和"caaaaaaandy"中前面的三个’a’,注意:即使"caaaaaaandy" 中有很多个’a’,但只匹配前面的三 个’a’即"aaa"。
 
字符[xyz]
意义:一字符列表,匹配列出中的任一字符。你可以通过连字符-指出一个字符范围。
例如:[abcd]跟[a-c]一样。它们匹配"brisket"中的’b’和"ache"中的’c’。
 
字符[^xyz]
意义:一字符补集,也就是说,它匹配除了列出的字符外的所有东西。 你可以使用连字符-指出一 字符范围。
例如:[^abc]和[^a-c]等价,它们最早匹配"brisket"中的’r’和"chop."中的’h’。
 
字符
意义:匹配一个空格(不要与b混淆)
 
字符b
意义:匹配一个单词的分界线,比如一个空格(不要与混淆)
例如:/bnw/匹配"noonday"中的’no’,/wyb/匹配"possibly yesterday."中的’ly’。
 
字符B
意义:匹配一个单词的非分界线
例如:/wBn/匹配"noonday"中的’on’,/yBw/匹配"possibly yesterday."中的’ye’。
 
字符cX
意义:这里的X是一个控制字符。匹配一个字符串的控制字符。
例如:/cM/匹配一个字符串中的control-M。
 
字符d
意义:匹配一个数字,等价于[0-9]。
例如:/d/或/[0-9]/匹配"B2 is the suite number."中的’2’。
 
字符D
意义:匹配任何的非数字,等价于[^0-9]。
例如:/D/或/[^0-9]/匹配"B2 is the suite number."中的’B’。
 
字符f
意义:匹配一个表单符
 
字符n
意义:匹配一个换行符
 
字符r
意义:匹配一个回车符
 
字符s
意义:匹配一个单个white空格符,包括空格,tab,form feed,换行符,等价于[ fnrtv]。
例如:/sw*/匹配"foo bar."中的’bar’。
 
字符S
意义:匹配除white空格符以外的一个单个的字符,等价于[^ fnrtv]。
例如:/S/w*匹配"foo bar."中的’foo’。
 
字符t
意义:匹配一个制表符
 
字符v
意义:匹配一个顶头制表符
 
字符w
意义:匹配所有的数字和字母以及下划线,等价于[A-Za-z0-9_]。
例如:/w/匹配"apple,"中的’a’,".28,"中的’5’和"3D."中的’3’。
 
字符W
意义:匹配除数字、字母外及下划线外的其它字符,等价于[^A-Za-z0-9_]。
例如:/W/或者/[^$A-Za-z0-9_]/匹配"50%."中的’%’。
 
字符n
意义:这里的n是一个正整数。匹配一个正则表达式的最后一个子串的n的值(计数左圆括号)。
 
例如:/apple(,)sorange1/匹配"apple, orange, cherry, peach."中的’apple, orange’,下面有一个更加完整的例子。
注意:如果左圆括号中的数字比n指定的数字还小,则n取下一行的八进制escape作为描述。
 
字符ooctal和xhex
意义:这里的ooctal是一个八进制的escape值,而xhex是一个十六进制的escape值,允许在一个正则表达式中嵌入ASCII码
 
附:下表是元字符及其在正则表达式上下文中的行为的一个完整列表:
 
字符 描述
\
将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。例如,'n' 匹配字符"n"。'\n' 匹配一个换行符。序列'\' 匹配"" 而"\(" 则匹配"("。
^
匹配输入字符串的开始位置。如果设置了RegExp 对象的Multiline 属性,^ 也匹配'\n' 或'\r' 之后的位置。
$
匹配输入字符串的结束位置。如果设置了RegExp 对象的Multiline 属性,$ 也匹配'\n' 或'\r' 之前的位置。
*
匹配前面的子表达式零次或多次。例如,zo* 能匹配"z" 以及"zoo"。* 等价于{0,}。
+ 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配"zo" 以及"zoo",但不能匹配"z"。+ 等价于{1,}。
?
匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配"do" 或"does" 中的"do" 。? 等价于{0,1}。
{n}
n 是一个非负整数。匹配确定的n 次。例如,'o{2}' 不能匹配"Bob" 中的'o',但是能匹配"food" 中的两个o。
{n,}
n 是一个非负整数。至少匹配n 次。例如,'o{2,}' 不能匹配"Bob" 中的'o',但能匹配"foooood" 中的所有o。'o{1,}' 等价于'o+'。'o{0,}' 则等价于'o*'。
{n,m}
m 和n 均为非负整数,其中n ?
当该字符紧跟在任何一个其他限制符(*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串"oooo",'o+?' 将匹配单个"o",而'o+' 将匹配所有'o'。
.
匹配除"\n" 之外的任何单个字符。要匹配包括'\n' 在内的任何字符,请使用象'[.\n]' 的模式。
(pattern)
匹配pattern 并获取这一匹配。所获取的匹配可以从产生的Matches 集合得到,在VBScript 中使用SubMatches 集合,在JScript 中则使用{CONTENT}… 属性。要匹配圆括号字符,请使用'\(' 或'\)'。
(?:pattern)
匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用"或" 字符(|) 来组合一个模式的各个部分是很有用。例如,'industr(?:y|ies) 就是一个比'industry|industries' 更简略的表达式。
(?=pattern)
正向预查,在任何匹配pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹配"Windows 2000" 中的"Windows" ,但不能匹配"Windows 3.1" 中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?!pattern)
负向预查,在任何不匹配Negative lookahead matches the search string at any point where a string not matching pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配"Windows 3.1" 中的"Windows",但不能匹配"Windows 2000" 中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
x|y
匹配x 或y。例如,'z|food' 能匹配"z" 或"food"。'(z|f)ood' 则匹配"zood" 或"food"。
[xyz]
字符集合。匹配所包含的任意一个字符。例如,'[abc]' 可以匹配"plain" 中的'a'。
[^xyz]
负值字符集合。匹配未包含的任意字符。例如,'[^abc]' 可以匹配"plain" 中的'p'。
[a-z]
字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配'a' 到'z' 范围内的任意小写字母字符。
[^a-z]
负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在'a' 到'z' 范围内的任意字符。
\b
匹配一个单词边界,也就是指单词和空格间的位置。例如,'er\b' 可以匹配"never" 中的'er',但不能匹配"verb" 中的'er'。
\B
匹配非单词边界。'er\B' 能匹配"verb" 中的'er',但不能匹配"never" 中的'er'。
\cx
匹配由x指明的控制字符。例如,\cM 匹配一个Control-M 或回车符。x 的值必须为A-Z 或a-z 之一。否则,将c 视为一个原义的'c' 字符。
\d
匹配一个数字字符。等价于[0-9]。
\D
匹配一个非数字字符。等价于[^0-9]。
\f
匹配一个换页符。等价于\x0c 和\cL。
\n
匹配一个换行符。等价于\x0a 和\cJ。
\r
匹配一个回车符。等价于\x0d 和\cM。
\s
匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
\S
匹配任何非空白字符。等价于[^ \f\n\r\t\v]。
\t
匹配一个制表符。等价于\x09 和\cI。
\v
匹配一个垂直制表符。等价于\x0b 和\cK。
\w
匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。
\W
匹配任何非单词字符。等价于'[^A-Za-z0-9_]'。
\xn
匹配n,其中n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,'\x41' 匹配"A"。'\x041' 则等价于'\x04' & "1"。正则表达式中可以使用ASCII 编码。.
\num
匹配num,其中num 是一个正整数。对所获取的匹配的引用。例如,'(.)' 匹配两个连续的相同字符。
\n
标识一个八进制转义值或一个后向引用。如果\n 之前至少n 个获取的子表达式,则n 为后向引用。否则,如果n 为八进制数字(0-7),则n 为一个八进制转义值。
\nm
标识一个八进制转义值或一个后向引用。如果\nm 之前至少有is preceded by at least nm 个获取得子表达式,则nm 为后向引用。如果\nm 之前至少有n 个获取,则n 为一个后跟文字m 的后向引用。如果前面的条件都不满足,若n 和m 均为八进制数字(0-7),则\nm 将匹配八进制转义值nm。
\nml
如果n 为八进制数字(0-3),且m 和l 均为八进制数字(0-7),则匹配八进制转义值nml。
\un
匹配n,其中n 是一个用四个十六进制数字表示的Unicode 字符。例如,\u00A9 匹配版权符号(?)。



摘自qeenoo的博客

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

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles