# (1)匹配 0 - 9 数字
pattern ="[0123456789]"
res = re.findall(pattern,'a b c d e f g h i j k 1 2 3 4 5')print(res)# ['1', '2', '3', '4', '5']
pattern ="[0-9]"
res = re.findall(pattern,'a b c d e f g h i j k 1 2 3 4 5')print(res)# ['1', '2', '3', '4', '5']# (2)匹配小写字母
pattern ="[abcd]"
res = re.findall(pattern,'a b c d e f g h i j k 1 2 3 4 5')print(res)# ['a', 'b', 'c', 'd']
pattern ="[a-z]"
res = re.findall(pattern,'a b c d e f g h i j k A B C D E 1 2 3 4 5')print(res)# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']# (3)匹配大写字母字符组
pattern ="[ABC]"
res = re.findall(pattern,'a b c d e f g h i j k A B C D E 1 2 3 4 5')print(res)# ['A', 'B', 'C']
pattern ="[A-Z]"
res = re.findall(pattern,'a b c d e f g h i j k A B C D E 1 2 3 4 5')print(res)# ['A', 'B', 'C', 'D', 'E']# (4)大小写字母+数字混合
pattern ="[0123abcdABC]"
res = re.findall(pattern,'a b c d e f g h i j k A B C D E 1 2 3 4 5')print(res)# ['a', 'b', 'c', 'd', 'A', 'B', 'C', '1', '2', '3']
pattern ="[0-9a-zA-Z]"
res = re.findall(pattern,'a b c d e f g h i j k A B C D E 1 2 3 4 5')print(res)# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'A', 'B', 'C', 'D', 'E', '1', '2', '3', '4', '5']
【3】元字符
元字符
匹配内容
.
匹配除换行符以外的任意字符
\w
匹配字母或数字或下划线
\s
匹配任意的空白符
\d
匹配数字
\n
匹配一个换行符
\t
匹配一个制表符
\b
匹配一个单词的结尾
^
匹配字符串的开始
$
匹配字符串的结尾
\W
匹配非字母或数字或下划线
\D
匹配非数字
\S
匹配非空白符
a|b
匹配字符a或字符b
()
匹配括号内的表达式,也表示一个组
[...]
匹配字符组中的字符
[^...]
匹配除了字符组中字符的所有字符
# 匹配特殊字符的时候# (1) . 代表除换行符以外的任意字符
letter ='a b c d e f g h i j k A B C D E 1 2 3 4 5 , '
pattern ="."
res = re.findall(pattern, letter)print(res)# (2)\w 代表数字或者字母或下划线
letter ='a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; % '
pattern ="\w"
res = re.findall(pattern, letter)print(res)# (3)\s 代表任意的空白符 ---> 空格
letter ='a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; % '
pattern ="\s"
res = re.findall(pattern, letter)print(res)# (4)\d 只能匹配数字
letter ='a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; % '
pattern ="\d"
res = re.findall(pattern, letter)print(res)# (5)\W 除了字母 或数字 或下滑线以外的任意字符
letter ='a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; % '
pattern ="\W"
res = re.findall(pattern, letter)print(res)# (6)\n 只能匹配换行符
letter ='''
' a
b c
d e f g h i j k A B C D E
1 2 3 4 5 , _ . ; %
'''
pattern ="\n"
res = re.findall(pattern, letter)print(res)# (7)\t 制表符
letter ='''a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; % '''
pattern ="\t"
res = re.findall(pattern, letter)print(res)# (8)\b 匹配一个单词的结尾# (9)^字符 以某个字符开头
letter ='''a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; %'''
pattern ="^a"
res = re.findall(pattern, letter)print(res)# (10) 字符$ 以某个字符结尾
letter ='''a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; %'''
pattern ="%$"
res = re.findall(pattern, letter)print(res)# (11)\D 匹配除数字以外的任意字符
letter ='''a b c d e f g h i j k A B C D E 1 2 3 4 5 , _ . ; %'''
pattern ="\D"
res = re.findall(pattern, letter)print(res)# (12)\S 匹配出了空格以外的所有字符
letter ='''a b c d e f g h
i j k A B C D E 1 2 3 4 5 , _ . ; %'''
pattern ="\S"
res = re.findall(pattern, letter)print(res)# (13) 字符|字符 匹配任意一个字符
letter ='''a b c d e f g h
i j k A B C D E 1 2 3 4 5 , _ . ; %'''
pattern ="a|b|1|%"
res = re.findall(pattern, letter)print(res)# (14) () 声明优先级
letter ='''a b c d e f g hi j k A B C D E 12 aa 34 5a , _ . ; %'''
pattern ="\d(\w)"
res = re.findall(pattern, letter)print(res)# (15)字符组
letter ='''a b c d e f g hi j k A B C D E 12 aa 34 5a , _ . ; %'''
pattern ="[a-z0-9A-Z][0-9]"# ['12', '34']# pattern = "[a-z0-9A-Z][a-z]"
res = re.findall(pattern, letter)print(res)
letter ='''a b c d e $f _g hi j k A B C D E 12 aa 34 5a , _ . ; %'''# 匹配除了字符组中字符的所有字符# $f _g hi 12 aa 34 5a
pattern ="[^a-z0-9A-Z][0-9]"# [' 1', ' 3', ' 5']
res = re.findall(pattern, letter)print(res)
【4】量词
量词
用法说明
*
重复零次或更多次
+
重复一次或更多次
?
重复零次或一次
{n}
重复n次
{n,}
重复n次或更多次
{n,m}
重复n到m次
# 代表当前字符的重复次数# (1) * 代表当前字符重读零次或更多次
pattern ="[0-9][a-z]*"#
letter ='''a b c d e $f _g hi j k A B C D E 12 aa 34 5a 111 6cs 9nb 6aaaa , _ . ; %'''
res = re.findall(pattern, letter)print(res)# (2) + 代表当前字符重读一次或更多次
pattern ="[0-9][a-z]+"#
letter ='''a b c d e $f _g hi j k A B C D E 12 aa 34 5a 111 6cs 9nb 6aaaa , _ . ; %'''
res = re.findall(pattern, letter)print(res)# (3)? 重复零次或一次
pattern ="[0-9][a-z]?"#
letter ='''a b c d e $f _g hi j k A B C D E 12 aa 34 5a 111 6cs 9nb 6aaaa , _ . ; %'''
res = re.findall(pattern, letter)print(res)# (4){n} 重复n次
pattern ="[0-9][a-z]{2}"#
letter ='''a b c d e $f _g hi j k A B C D E 12 aa 34 5a 111 6cs 9nb 6aaaa , _ . ; %'''
res = re.findall(pattern, letter)print(res)# (4){n,m} 重复至少n次 至多m次
pattern ="[0-9][a-z]{2,3}"#
letter ='''a b c d e $f _g hi j k A B C D E 12 aa 34 5a 111 6cs 9nb 6aaaa , _ . ; %'''
res = re.findall(pattern, letter)print(res)
【5】位置元字符
import re
# . 代表任意字符
pattern ="海."
letter ='''海燕海娇海东海冬梅'''
res = re.findall(pattern, letter)# ['海燕', '海娇', '海东', '海冬']print(res)# . 代表任意字符# ^ 以 ... 开头
pattern ="^海."
letter ='''海燕海娇海东海冬梅'''
res = re.findall(pattern, letter)# ['海燕']print(res)# # . 代表任意字符# # $ 以 ... 结尾
pattern ="海.$"
letter ='''海燕海娇海东海冬梅'''
res = re.findall(pattern, letter)# ['海冬']print(res)# . 代表任意字符# ? 重复零次或一次
pattern ="李.?"
letter ='李杰李莲英和李二棍子'
res = re.findall(pattern, letter)# ['李杰', '李莲', '李二']print(res)# . 代表任意字符# * 重复零次或更多次
pattern ="李.*"
letter ='李杰李莲英和李二棍子'
res = re.findall(pattern, letter)# ['李杰李莲英和李二棍子']print(res)# . 代表任意字符# + 重复一次或更多次
pattern ="李.+"
letter ='李杰李莲英和李二棍子'
res = re.findall(pattern, letter)# ['李杰李莲英和李二棍子']print(res)# . 代表任意字符# {m,n} 重复一次或更多次
pattern ="李.{2,3}"
letter ='李杰李莲英和李二棍子'
res = re.findall(pattern, letter)# ['李杰李莲', '李二棍子']print(res)# . 代表任意字符# [] 字符组中的任意一个
pattern ="李[杰莲英二棍子]"
letter ='李杰李莲英和李二棍子'
res = re.findall(pattern, letter)# ['李杰', '李莲', '李二']print(res)# . 代表任意字符# [] 字符组中的任意一个# ^和 除了和# * 任意
pattern ="李[^和]*"
letter ='李杰李莲英和李二棍子'
res = re.findall(pattern, letter)# ['李杰李莲英', '李二棍子']print(res)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY