正则表达式的语法

目录
  1. 1. 元字符
  2. 2. 量词
  3. 3. 捕获

元字符

我们使用的字符“[”和“]”叫元字符,对模式有特殊的效果。这种元字符共有11个,它们扮演着不同的角色。如果想在建立的模式中包含这些元字符中的某一个字符,需要在该字符前使用转义字符“\”。

元字符 描述
^ 开始
(beginning)
字符^之后的实体(entity),必须在配匹配的字符串开始部分找到。
例:
^h
能匹配的字符串:hello,h,hh
不能匹配的字符串:character,ssh
$ 结束
(end)
字符$之前的实体(entity),必须在配匹配的字符串尾部找到。
例:
e$
能匹配的字符串:sample,e,file
不能匹配的字符串:extra,shell
. 任何字符
(any)
匹配任何字符。
例:
hell.
能匹配的字符串:hello,hellx,hell5,hell!
不能匹配的字符串:hell,helo
[]
(set)
匹配指定字符集内的任何字符。
语法:[a-z]表示一个区间,[abcd]表示一组,[a-z0-9]表示两个区间。
例:
hell[a-y123]
能匹配的字符串:hello,hell1,hell2
不能匹配的字符串:hellz,hell4,heloo
[^] 否定组
(negate set)
匹配任何不包括在指定字符集内的字符串
例:
hell[^a-np-z0-9]
能匹配的字符串:hello,hell
不能匹配的字符串:hella,hell5

(alternation)
匹配符号之前或之后的实体。
例:
hello|welcome
能匹配的字符串:hello,welcome,helloes,awelcome
不能匹配的字符串:hell,ellow,owelcom
() 分组
(grouping)
组成一组用于匹配的实体(entity),常用符号协助完成。
例:
^(hello|hi)there$
能匹配的字符串:hello there,hi there
不能匹配的字符串:hey there,ahoy there
\ 转义
(escape)
允许你对一个特殊字符转义(即将具有特殊意义的字符变为普通的文本字符)
例:
hello\.
能匹配的字符串:hello.,hello...
不能匹配的字符串:hello,hella

量词

可以通过使用有限的字符来表达简单的匹配。下表所示的量词允许你扩展实体匹配使用量词可以定制匹配的次数。

量词 描述
*
0或多次
字符*之前的实体被发现0次或多次。
例:
he*llo
能匹配的字符串:hllo,hello,heeeello
不能匹配的字符串:hallo,ello
+
1或多次
字符+之前的实体被发现1次或多次。
例:
he+llo
能匹配的字符串:hello,heeello
不能匹配的字符串:hllo,helo
?
0或1次
字符?之前的实体被发现0次或1次。
例:
he?llo
能匹配的字符串:hello,hllo
不能匹配的字符串:heello,heeeello
{x}
x次
字符{}之前的字符必须匹配指定的x次数。
例:
he{3}llo
能匹配的字符串:heeello,oh heeello
不能匹配的字符串:hello,heello,heeeello
{x,}
至少x次
字符{}之前的字符必须至少出现x次。(即可以多于x次)
例:
he{3}llo
能匹配的字符串:heeello,heeeeello
不能匹配的字符串:hllo,hello,heello
{x,y}
x到y次
字符{}之前的字符出现的次数必须介于x和y次之间。
例:<br>he{2,4}llo
能匹配的字符串:heello,heeello,heeeello
不能匹配的字符串:hello,heeeeello

捕获

正则表达式机制最后一个功能是能够捕获子表达式,放在“()”之间的任何文本,在被捕获后都能用于后面的匹配处理。

模式 字符串 捕获
^(hello|hi)(sir|mister)$ hello sir $1 = hello
$2 = sir
^(.*)$ nginx rocks $1 = nginx rocks
^(.{1,3})([0-9]{1,4})([?!]{1,2})$ abc1234?! $1 = abc
$2 = 1234
$3 = ?!