22 - 正則基礎📄目錄1 正則表達式1.1 特點1.2 使用步驟2 匹配單個字符2.1 .z 匹配任意字符(常用)2.2 [] 匹配列舉的字符(常用)2.3 \d 匹配數字0-9(常用)2.4 \D 匹配非數字(常用)2.5 \s 匹配空白,即空格和tab鍵2.6 \S 匹配非空白2.7 \w 匹配單詞字符,即a-Z,A-Z,0-9,_,漢字(常用)2.8 \W 匹配非單詞字符3 匹配多個字符3.1 * 匹配前一個字符出現不限次數(含0次)(常用)3.2 + 匹配前一個字符出現至少一次(常用)3.3 ? 四配前一個字符出現1次或者0次3.4 {m} 匹配前一個字符出現m次3.5 {m,n} 匹配前一個字符出現從m到n次4 匹配開頭結尾4.1 ^ 匹配字符串開頭/表示對...取反4.2 $ 匹配以...結尾導航連結:
字符串處理工具
❗️ 注意:使用時需要導入re模塊
語法比較複雜,可讀性較差
通用性很強,適用於多種語言
導入re模塊
使用match方法進行匹配操作
re.match()能匹配出以xxx開頭的字符串,❗從字符串開頭開始匹配❗
如果起始位置沒有匹配成功,返回None
如果上一步數據匹配成功,使用group()提取數據
re.match(pattern, string, flags)
pattern:匹配的正則表達式
string:要匹配的字符串
flags:標誌位,主要用於是否區分大小寫,多行匹配等
❗❗ 如果想在字符串任何位置匹配,可以用 re.search()

xxxxxxxxxxres = re.match("18", "18-year-old")# ❗從字符串開頭開始匹配❗匹配不到就沒有❗print(res)# 如果上一步數據匹配成功,使用group()提取數據print(res.group())輸出結果:
xxxxxxxxxx<re.Match object; span=(0, 2), match='18'>18❗ 匹配的是表達式整體
| 字符 | 功能 |
|---|---|
| . | 匹配任意1個字符(除了\n) |
| [ ] | 匹配[ ]中列舉的字符 |
| \d | 匹配數字,即0至9 |
| \D | 匹配非數字,即不是數字 |
| \s | 匹配空白,即空格、tab鍵 |
| \S | 匹配非空白 |
| \w | 匹配單詞字符,即a-z、A-Z、0-9、_ |
| \W | 匹配非單詞字符 |
.z 匹配任意字符(常用)⚠️ 不能匹配\n

xxxxxxxxxxres = re.match("..","hello") # 每個點是一個字符print(res.group())輸出結果:
xxxxxxxxxxhe[] 匹配列舉的字符(常用)
xxxxxxxxxxres1 = re.match("[he]", "hello") # 匹配h和eprint(res1.group(), end = " | ")res2 = re.match("[12][23]", "1234") # 分別匹配12及23print(res2.group(), end = " | ")res3 = re.match("[0-9]", "50723") # 匹配0-9print(res3.group(), end = " | ")# res3 = re.match("[0-469]", "50723") # 報錯res4 = re.match("[a-zA-Z]", "Antonio") # 匹配所有大小寫字母print(res4.group(), end = " | ")輸出結果:
xxxxxxxxxxh | 12 | 5 | A | \d 匹配數字0-9(常用)
xxxxxxxxxxres = re.match(".\d\d", "s97S43") # 例子:匹配任意字符+兩個數字print(res.group())輸出結果:
xxxxxxxxxxs97\D 匹配非數字(常用)能匹配任何字母、符號(只有0-9不能被匹配)

xxxxxxxxxxres = re.match(".\D\D", "7S<43") # 例子:匹配任意字符+兩個非數字print(res.group())輸出結果:
xxxxxxxxxx7S<\s 匹配空白,即空格和tab鍵
xxxxxxxxxxres = re.match("\s.\s.", " h ello")print(res.group())輸出結果:
xxxxxxxxxx h e\S 匹配非空白
xxxxxxxxxxres = re.match("\S\s..", "T Tki")print(res.group())輸出結果:
xxxxxxxxxxT Tk\w 匹配單詞字符,即a-Z,A-Z,0-9,_,漢字(常用)
xxxxxxxxxxres = re.match("\w\w[a-z][A-Z]", "今天sTrii")print(res.group())輸出結果:
xxxxxxxxxx今天sT\W 匹配非單詞字符
xxxxxxxxxxres = re.match("\W\W\W", "<?.ITS")print(res.group())輸出結果:
xxxxxxxxxx<?.| 字符 | 功能 |
|---|---|
| * | 匹配前一個字符出現0次或者無限次,即可有可無 |
| + | 匹配前一個字符出現1次或者無限次,即至少有一次 |
| ? | 四配前一個字符出現1次或者0次,即要麼有1次,要麼沒有 |
| {m} | 匹配前一個字符出現m次 |
| {m,n} | 匹配前一個字符出現從m到n次 |
* 匹配前一個字符出現不限次數(含0次)(常用)
xxxxxxxxxxres = re.match("\w*","Antony is sleeping.")print(res.group())輸出結果:
xxxxxxxxxxAntony+ 匹配前一個字符出現至少一次(常用)
xxxxxxxxxxres = re.match("\d+", "12我在學編程")print(res.group())輸出結果:
xxxxxxxxxx12? 四配前一個字符出現1次或者0次
xxxxxxxxxxres = re.match("\d?","12hello")print(res.group())輸出結果:
xxxxxxxxxx1{m} 匹配前一個字符出現m次
xxxxxxxxxxres = re.match("\w{3}", "python")print(res.group())輸出結果:
xxxxxxxxxxpyt{m,n} 匹配前一個字符出現從m到n次
xxxxxxxxxxres = re.match("\w{3,5}", "python")print(res.group())輸出結果:
xxxxxxxxxxpytho| 字符 | 功能 |
|---|---|
| ^ | 匹配字符串開頭 |
| $ | 匹配字符串結尾 |
^ 匹配字符串開頭/表示對...取反
xxxxxxxxxxres = re.match("^py","python")print(res.group())輸出結果:
xxxxxxxxxxpy
❗ 注意:^在[]中表示不匹配
xxxxxxxxxxres1 = re.match("[^py]","thon") # 只要在中括號裡就不能匹配,p和y都不能有print(res1.group())輸出結果:
xxxxxxxxxxt$ 匹配以...結尾
xxxxxxxxxxres = re.match(".*o$" ,"Antonio") # .*表示前面有不限制個字符res1 = re.match(".{7}\w$" ,"Victoria")print(res.group())print(res1.group())輸出結果:
xxxxxxxxxxAntonioVictoria