1. 匹配单个字符

Char Function
. 匹配任意1个字符(除了\n)
[ ] 匹配[ ]中列举的字符
\d 匹配数字,即0-9
\D 匹配非数字,即不是数字
\s 匹配空白,即 空格,tab键
\S 匹配非空白
\w 匹配单词字符,即a-z、A-Z、0-9、_
\W 匹配非单词字符

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import re
ret = re.match(".","M")
print(ret.group()) #M

ret = re.match("t.o","too")
print(ret.group()) #too

ret = re.match("[hH]ello","hello Python")
print(ret.group()) # hello

ret = re.match("[0-9]Hello Python","7Hello Python”)
print(ret.group()) # 7Hello Python

ret = re.match("[0-3,5-9]Hello Python","7Hello Python")
print(ret.group()) # 7Hello Python

ret = re.match("嫦娥\d号","嫦娥1号发射成功")
print(ret.group()) #嫦娥1号

2. 匹配多个字符

Char Function
* 匹配前一个字符出现0次或者无限次,即可有可无
+ 匹配前一个字符出现1次或者无限次,即至少有1次
? 匹配前一个字符出现1次或者0次,即要么有1次,要么没有
{m} 匹配前一个字符出现m次
{m,n} 匹配前一个字符出现从m到n次

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re
ret = re.match("[A-Z][a-z]*","M")
print(ret.group()) #M

ret = re.match("[A-Z][a-z]*","MnnM")
print(ret.group()) #Mnn

ret = re.match("[1-9]?[0-9]","7")
print(ret.group()) #7

ret = re.match("[1-9]?\d","33")
print(ret.group()) #33

ret = re.match("[1-9]?\d","09")
print(ret.group()) #None

ret = re.match("[a-zA-Z0-9_]{6}","12a3g45678")
print(ret.group()) #12a3g4

ret = re.match("[a-zA-Z0-9_]{8,20}","1ad12f23s34455ff66")
print(ret.group()) #1ad12f23s34455ff66

3. 获取子串

String Function
(.+?) 惰性匹配,从左到右匹配,并且至少有一个字符
(.+) 贪婪匹配,整个字符串是否匹配,并且至少有一个字符
(.*?) 惰性匹配,从左到右匹配
(.*) 贪婪匹配,整个字符串是否匹配

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import re
txt = "<111222><3334444>"
ret = re.findall(r"<(.+?)>", txt)
# 返回 111222 和 3334444

txt = "<111222><3334444>"
ret = re.findall(r"<(.+)>", txt)
# 返回 111222><3334444

txt = "<111222><3334444>"
ret = re.findall(r"<.+>", txt)
# 返回 <111222><3334444>

txt = "<><3334444><555666>"
ret = re.findall(r"<(.*?)>", txt)
# 返回 ""和3334444和555666

txt = "<><3334444><555666>"
ret = re.findall(r"<(.+?)>", txt)
# 返回 ><3334444和555666