Table of Contents
字符
字符(x)
字符x│y
字符{ n, }
字符{ n,m }
字符[^xyz]
字符B
字符cX
字符d
字符D
字符f
字符S
字符t
字符W
字符n
字符ooctal和xhex
Home Backend Development PHP Tutorial 正则表达式的特殊字符含义

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

Jun 20, 2016 pm 01:04 PM
apple quot

今天看到一篇关于正则表达式的比较好的文章,讲的是正则表达式的特殊字符含义,感觉非常不错,遂记录一下。

字符/

意义:对于字符,通常表示按字面意义,指出接着的字符为特殊字符。
例如:/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码。


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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Aug 18, 2024 am 06:34 AM

Update: Saunders Tech has uploaded a tutorial to his YouTube channel (video embedded below) explaining how to install Fortnite and the Epic Games Store on an iPad outside the EU. However, not only does the process require specific beta versions of iO

Apple\'s \'HomeAccessory\' device revealed to have an A18 chipset among other features Apple\'s \'HomeAccessory\' device revealed to have an A18 chipset among other features Sep 27, 2024 am 09:02 AM

Fresh details of Apple's HomePod-like device with an integrated screen have surfaced and they paint a clearer picture of the device which has been referred to as 'HomeAccessory'. When it launches, it will be Apple's answer to Google's Nest Hub Max an

iPhone 16 Pro and iPhone 16 Pro Max official with new cameras, A18 Pro SoC and larger screens iPhone 16 Pro and iPhone 16 Pro Max official with new cameras, A18 Pro SoC and larger screens Sep 10, 2024 am 06:50 AM

Apple has finally lifted the covers off its new high-end iPhone models. The iPhone 16 Pro and iPhone 16 Pro Max now come with larger screens compared to their last-gen counterparts (6.3-in on the Pro, 6.9-in on Pro Max). They get an enhanced Apple A1

Apple iPhone 16 and iPhone 16 Plus launched with 48MP \'Fusion camera\', Camera Control and A18 chip Apple iPhone 16 and iPhone 16 Plus launched with 48MP \'Fusion camera\', Camera Control and A18 chip Sep 10, 2024 am 09:30 AM

Apple has officially announced the iPhone 16 and iPhone 16 Plus, introducing key hardware updates with the new A18 chip. Both models come in two sizes—6.1 inches and 6.7 inches—with Super Retina XDR displays. They also feature aluminum designs and ar

New Apple iPhone 16 design and colours shown in comparison video against iPhone 15 New Apple iPhone 16 design and colours shown in comparison video against iPhone 15 Aug 12, 2024 am 06:59 AM

The launches of Apple's next iPhones are not expected for at least another month. Nonetheless, footage showing dummy iPhone 16 units in what are said to be official launch colours continues to emerge online. Incidentally, while Google previously rele

Apple analyst offers new insights into upcoming iPhone 16, iPhone SE 4 and even rumoured iPhone 17 Air releases Apple analyst offers new insights into upcoming iPhone 16, iPhone SE 4 and even rumoured iPhone 17 Air releases Aug 12, 2024 pm 10:01 PM

Apple's next-generation iPhones are right around the corner. While the company has not shared the date of a new launch event yet, all signs appear to point to a September release, just like last year's iPhone 15 series. Incidentally, footage of dummy

Analyst discusses launch pricing for rumoured Meta Quest 3S VR headset Analyst discusses launch pricing for rumoured Meta Quest 3S VR headset Aug 27, 2024 pm 09:35 PM

Over a year has now passed from Meta's initial release of the Quest 3 (curr. $499.99 on Amazon). Since then, Apple has shipped the considerably more expensive Vision Pro, while Byte Dance has now unveiled the Pico 4 Ultra in China. However, there is

Pixel 9 Pro XL vs iPhone 15 Pro Max camera comparison reveals surprising Google wins in video and zoom performance Pixel 9 Pro XL vs iPhone 15 Pro Max camera comparison reveals surprising Google wins in video and zoom performance Aug 24, 2024 pm 12:32 PM

The Google Pixel 9 Pro and Pro XL are Google's answers to the likes of the Samsung Galaxy S24 Ultra and the Apple iPhone 15 Pro and Pro Max. Daniel Sin on YouTube(watch below) has compared the Google Pixel 9 Pro XL to the iPhone 15 Pro Max with some

See all articles