一篇文章搞清楚python中的正则
概览
正则表达式介绍:
概述:
正确的, 符合特定规则的 字符串.
Regular Expression, 正则表达式, 简称: re
细节:
1. 学正则表达式, 就是学正则表达式的规则,看懂别人写的式子, 且会简单修改.
2. 正则不独属于Python, 像Java, JavaScript, PHP, Go等都支持.
步骤:
1. 导包
import re
2. 正则匹配
result = re.match('正则表达式', '要校验的字符串') 从前往后依次匹配,只要能匹配即可.
result = re.search('正则表达式', '要校验的字符串') 分段查找.
3. 获取匹配结果.
result.group()
正则常用的规则:
. 代表任意的 1个字符, 除了
. 取消.的特殊含义, 就是1个普通的.(转义)
a 代表1个普通的字符 a
[abc] 代表a,b,c中任意的1个字符
[^abc] 代表除了a,b,c外, 任意的1个字符
d 代表数字, 等价于 [0-9]
D 代表非数字, 等价于 [^0-9]
s 代表空白字符, 等价于 [
]
S 代表非空白字符
w 代表非特殊字符, 即: 数字, 字母, 下划线, 汉字, [a-zA-Z0-9_汉字]
W 代表特殊字符, 非字母,数字,下划线,汉字
^ 表示开头
$ 表示结尾
* 表示前面出现的内容 0~无数 次
? 表示前面出现的内容 0~1 次
+ 表示前面出现的内容 1~无数 次
{n} 表示前面出现的内容 n 次
{n,} 表示前面出现的内容最少n次,最多无数次
{n,m} 表示前面出现的内容最少n次,最多m次
| 代表 或者的意思
() 代表分组,从左往右第几个(代表第几组
um 代表引用第几组的内容
(?P<分组名>) 设置分组并命名
(?P=分组名) 引用命名后的分组
案例1:正则校验单个字符
# 1.导包
import re
# 2.正则校验, 参1: 正则规则, 参2: 要被校验的字符串
# result = re.match('.it', 'ait') # 匹配成功
# result = re.match('.it', '你it') # 匹配成功
# result = re.match('.it', '你好it') # 失败
# result = re.match('.it', '你it') # 失败
# result = re.match('.it', '.it') # 匹配成功
result = re.match('[ahg]it', 'ait') # 匹配成功
result = re.match('[ahg]it', 'hit') # 匹配成功
result = re.match('[ahg]it', 'git') # 匹配成功
result = re.match('[ahg]it', 'g it') # 失败
result = re.match('[^ahg]it', 'ait') # 失败
result = re.match('[^ahg]it', 'x it') # 失败
result = re.match('[^ahg]it', 'xit') # 匹配成功
result = re.match('[^ahg]it', 'xitabcxyz') # 匹配成功, 从前往后匹配, 匹配到就返回.
result = re.match('[^ahg]it', 'abcxitabcxyz') # 失败, 从前往后依次查找.
# result = re.search('[^ahg]it', 'abcxitabcxyz') # 匹配成功,从前往后依次查找.
result = re.match('[3-7]it', '3it') # 匹配成功
result = re.match('[3-7]it', '-it') # 失败, [3-7]等价于[34567]
result = re.match('adab', 'a1ab') # 匹配成功
result = re.match('adab', 'a10ab') # 失败
result = re.match('aDab', 'a!ab') # 匹配成功
result = re.match('aDab', 'abab') # 匹配成功
result = re.match('asab', 'abab') # 失败
result = re.match('asab', 'a ab') # 匹配成功
result = re.match('asab', 'a
ab') # 匹配成功
result = re.match('asab', 'a ab') # 匹配成功
result = re.match('awab', 'a ab') # 失败
result = re.match('awab', 'a!ab') # 失败
result = re.match('awab', 'axab') # 匹配成功
result = re.match('awab', 'a_ab') # 匹配成功
result = re.match('awab', 'a6ab') # 匹配成功
result = re.match('awab', 'aYab') # 匹配成功
result = re.match('awab', 'a我ab') # 匹配成功
# 3.获取匹配结果.
if result:
print(result.group())
else:
print('匹配失败')
案例2:正则替换
# 导包
import re
# 1.定义字符串.
s = '开心你就大声笑,哈哈,呵呵,嘿嘿,嘻嘻,桀桀桀,啦啦啦'
# 2.把上述的 哈,呵,嘿,嘻,桀 替换为 ♥
# 正则规则 新字符串 要被替换的字符串
result = re.compile('哈|呵|嘿|嘻|桀').sub('♥', s)
# 3.打印结果.
print(result)
print('-' * 23)
# 新版API(函数)的写法.
# 参1: 正则规则, 参2: 新字符串, 参3: 要被替换的字符串
result = re.sub('哈|呵|嘿|嘻|桀', '♣', s)
print(result)
案例3:正则校验多个字符
# 导包
import re
# 验证 * 代表前边的内容 出现至少0次, 至多无数次
result = re.match('.*xy.*', 'abcxy123') # 匹配成功
result = re.match('.*xy.*', 'xy123') # 匹配成功
result = re.match('.*xy.*', 'abcxy') # 匹配成功
result = re.match('.+xy.*', 'abcxy') # 匹配成功
result = re.match('.+xy.*', 'xy123') # 失败
result = re.match('.?xy.*', 'axy123') # 匹配成功
result = re.match('.?xy.*', 'xy123') # 匹配成功
result = re.match('.?xy.*', 'abcxy123') # 失败
result = re.match(r'd{3}xyw{2,5}', '123xy123') # 匹配成功
result = re.match(r'd{3}xyw{2,5}', '123xy12@') # 匹配成功
result = re.match(r'd{3}xyw{2,5}', '123xyabcAB') # 匹配成功
result = re.match(r'd{3}xyw{2,5}', '1234xy123') # 失败
result = re.match(r'd{3}xyw{2,5}', '12xy123') # 失败
result = re.match(r'd{3}xyw{2,5}', '123xy1@') # 失败
result = re.match(r'd{3}xyw{2,5}', '123xyabcAB1') # 失败
result = re.match(r'd{3,}xyw{2,5}', '12xyabcAB1') # 失败
result = re.match(r'd{3,}xyw{2, 5}', '123xyabcAB1') # 失败, 注意空格
result = re.match(r'd{3,}xyw{2,5}', '123xyabc') # 匹配成功
# 验证 ? 代表前边的内容 出现至少0次, 至多1次
# 验证 + 代表前边的内容 出现至少1次, 至多无数次
# 验证 {n} 代表前边的内容 恰好出现n次, 多一次,少一次都不行
# 验证 {n,} 代表前边的内容 至少出现n次, 至多无数次
# 验证 {n,m} 代表前边的内容 至少出现n次, 至多出现m次, 包左包右.
# 查看结果.
print(result.group() if result else '未匹配')
案例4:正则校验开头和结尾
# 导包
import re
# 正则匹配
# 需求1: 校验字符串必须以数字开头, 无论match(), 还是search()均是. 后边是啥无所谓.
# result = re.match(r'd+.*', 'abc123xyz') # 失败
# result = re.search(r'd+.*', 'abc123xyz') # 匹配成功
#
# result = re.match(r'^d+.*', 'abc123xyz') # 失败
# result = re.search(r'^d+.*', 'abc123xyz') # 失败
# 需求2: 校验字符串必须以数字开头, 以任意的3个字母结尾.
# result = re.search(r'^d+.*[a-zA-Z]{3}', 'abc123xyz12') # 失败
# result = re.search(r'^d+.*[a-zA-Z]{3}', '123你好xyz12') # 匹配成功
# result = re.search(r'^d+.*[a-zA-Z]{3}$', '123你好abc12') # 失败
# result = re.search(r'^d+.*[a-zA-Z]{3}$', '123你好abc') # 匹配成功
# 需求3: 校验手机号. 规则: 1.长度必须是11位 2.必须是纯数字. 3.第1位数字必须是1. 4.第2位数字可以是 3-9
result = re.match(r'^1[3-9]d{9}$', '13112345678a')
result = re.match(r'^1[3-9]d{9}$', '12112345678')
result = re.match(r'^1[3-9]d{9}$', '13112345678')
# 打印匹配结果.
print(result.group() if result else '未匹配!')
案例5:正则表达式校验分组
# 导包
import re
# 需求: 在列表 fruits = ['apple', 'banana', 'orange', 'pear'], 匹配 apple, pear
# 1.定义水果列表
fruits = ['apple', 'banana', 'orange', 'pear']
# 2. 遍历, 获取到每种水果.
for fruit in fruits:
# 3. 判断当前水果是否是 喜欢吃的水果.
# 参1: 正则表达式, 参2: 要校验的字符串.
if re.match('apple|pear', fruit):
# 4. 走这里, 说明是喜欢吃的.
print(f'喜欢吃: {fruit}')
else:
# 5. 走这里, 说明不是喜欢吃的.
print(f'不喜欢吃: {fruit}')
案例6:正则表达式校验邮箱
# 导包
import re
# 1. 定义邮箱.
email = "abcd@163.com"
# 2. 校验邮箱是否合法.
result = re.match(r'^[a-zA-Z_0-9]{4,20}@(163|126|qq).(com|cn)$', email)
# 3. 打印结果.
if result:
print(f'合法邮箱为: {result.group()}')
print(f'合法邮箱为: {result.group(0)}') # 获取第0组的信息, 效果同上, 即: 整个匹配到的结果.
print(f'合法邮箱为: {result.group(1)}') # 获取第1组的信息, 即: 163
print(f'合法邮箱为: {result.group(2)}') # 获取第2组的信息, 即: com、cn
else:
print("邮箱不合法!")
案例7:提取QQ号
import re
# 需求: 数据格式为 qq:数字, 从中提qq文本 和 qq号
# 1.定义变量, 记录要校验的字符串.
s = 'qq:123456'
# 2.正则校验.
result = re.match(r'^(qq):(d{6,11})$', s)
# 3.提取内容.
if result:
print(result.group())
print(result.group(0)) # 效果同上.
print('-' * 23)
print(result.group(1))
print(result.group(2))
else:
print('未匹配')
案例8:校验html标签
import re
# 需求1: 校验html的单级标签.
# 1.定义变量, 记录: html标签.
# html_s = '我是html页面' # 字母数: 1 ~ 4
# 2.匹配校验.
# 写法1: 重新copy一份.
# result = re.match('<[a-zA-Z]{1,4}>.*[a-zA-Z]{1,4}>', html_s)
# 写法2: 引入分组的概念.
# result = re.match(r'<([a-zA-Z]{1,4})>.*>', html_s)
# 3.打印结果.
# if result:
# print(result.group())
# else:
# print('未匹配!')
# 需求2: 校验html的多级标签.
# 1.定义变量, 记录: html标签.
html_s = '我是html页面
' # 字母数: 1 ~ 4, 标题标签1 ~ 6
# 2.匹配校验.
# 写法1: 重新copy一份.
# result = re.match(r'<[a-zA-Z]{1,4}>.* [a-zA-Z]{1,4}>', html_s)
# 写法2: 引入分组的概念.
# result = re.match(r'<([a-zA-Z]{1,4})><(h[1-6])>.*>>', html_s)
# 写法3: 给分组起名.
result = re.match(r'<(?P[a-zA-Z]{1,4})><(?Ph[1-6])>.*(?P=B)>(?P=A)>', html_s)
# 3.打印结果.
if result:
print(result.group())
print(result.group('A'))
print(result.group('B'))
else:
print('未匹配!')
以上内容仅供参考,有什么问题,小伙伴们可以在评论区留言哈







