当前位置: 首页 > 图灵资讯 > 行业资讯> Python正则表达式实现非捕获分组

Python正则表达式实现非捕获分组

发布时间:2024-06-24 13:44:54

1、有时我不想引用子表达式匹配结果,也不想捕捉匹配结果,只是把小括号作为一个整体来匹配。

2、非捕获分组可以在组的开头使用,非捕获分组可以实现。

实例

importre

s='img1.jpg,img2.jpg,img3.bmp'

#捕获分组
p=r'\w+(\.jpg)'
mlist=re.findall(p,s)①
print(mlist)

#非捕获分组
p=r'\w+(?:\.jpg)'
mlist=re.findall(p,s)②
print(mlist)

输出

['.jpg','.jpg']
['img1.jpg','img2.jpg']

以上是Python正则表达式实现非捕获分组,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09