python正则表达式re 中m.group和m.groups的解释,m.groupm.groups,转载:http://
python正则表达式re 中m.group和m.groups的解释,m.groupm.groups,转载:http://
转载:http://www.cnblogs.com/kaituorensheng/archive/2012/08/20/2648209.html
先看代码instance:
1 >>> a="123abc456" 2 >>> import re 3 >>> print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0)) 4 123abc456 5 >>> print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1)) 6 123 7 >>> print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2)) 8 abc 9 >>> print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(3))10 45611 >>> print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).group())12 123abc45613 >>> print(re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups())14 (‘123‘, ‘abc‘, ‘456‘)15 >>> print(re.search("([0-9])*([a-z])*([0-9]*)", a).groups())16 (‘3‘, ‘c‘, ‘456‘)17 >>> print(re.search("([0-9])*([a-z])*([0-9]*)", a).groups(1))18 (‘3‘, ‘c‘, ‘456‘)19 >>> print(re.search("([0-9])*([a-z])*([0-9]*)", a).group(0))20 123abc45621 >>> print(re.search("([0-9])*([a-z])*([0-9]*)", a).group())22 123abc456
究其因:
1. 正则表达式中的三组括号把匹配结果分成三组
m.group() == m.group(0) == 所有匹配的字符(即匹配正则表达式整体结果)group(1) 列出第一个括号匹配部分,group(2) 列出第二个括号匹配部分,group(3) 列出第三个括号匹配部分。m.groups() 返回所有括号匹配的字符,以tuple格式。m.groups() == (m.group(0), m.group(1), ...)2. 没有匹配成功的,re.search()返回None
3. 当然郑则表达式中没有括号,group(1)肯定不对了。
python正则表达式re 中m.group和m.groups的解释
相关内容
- Python 使用 Matplotlib 做图时,如何画竖直和水平的分割线
- Python结合SAP GUI Script操作sap的简易教程,pythonsap, 众
- python 简单爬虫(beatifulsoup),pythonbeatifulsoup,---恢复内容
- Python的X[y==1, 0],python,最近研究逻辑回归,I
- Python3基础 小技巧 为模块更换名字 import 模块名 as 别名
- python3使用ddt框架进行外部传参,python3ddt,ddt:python
- 四十六 Python分布式爬虫打造搜索引擎Scrapy精讲—elas
- Python pandas.DataFrame调整列顺序及修改index名,,1. 从字典
- python匹配两个字符串中间的字符串,python匹配,问题:使
- python中使用指定GPU,python使用指定GPU,import oso
评论关闭