python中group和groups函数的区别
2019-08-11 20:03
836 查看
原文链接:https://blog.csdn.net/qq_33472765/article/details/80803227?utm_source=copy
python中的group方法
group()在正则表达式中用于获取分段截获的字符串,解释如下代码(代码来自网络):import rea = “123abc456”
print re.search("([0-9])([a-z])([0-9])",a).group(0) #123abc456,返回整体
print re.search("([0-9])([a-z])([0-9])",a).group(1) #123
print re.search("([0-9])([a-z])([0-9])",a).group(2) #abc
print re.search("([0-9])([a-z])([0-9])",a).group(3) #456
可以看出,正则表达式按照数字-字母-数字的顺序来获取相应字符串,那么分别就是“数字
(group(1))–字母(group(2))–数字(group(3))”的对应关系,其中,group
(0)和group()效果相同,均为获取取得的字符串整体。group和groups是两个不同的函
数。一般,m.group(N) 返回第N组括号匹配的字符。而m.group() == m.group(0) == 所有匹
配的字符,与括号无关,这个是API规定的。m.groups() 返回所有括号匹配的字符,以tuple
格式。m.groups() == (m.group(1), m.group(2) …)
如图:
ttps://www.jianshu.com/p/46a7a630ecf1
相关文章推荐
- Python match-search-findall-group(s)的区别
- python group() 和 groups()的区别
- 【python】正则表达式-group和group的区别
- python 正则表达式 groups和group有什么区别
- 有没有一个工具可以帮助查找python的bug和进行静态的代码区别
- Python之2与3_is与==的区别_小数据池
- python 列表和元组的区别
- Python2.x与Python3.x版本区别
- python中staticmethod和classmethod的区别
- Python中__repr__和__str__区别
- Python2和3字符编码的区别知识点整理
- python2 与 python3的区别
- python中单,双引号和三个双引号的区别
- python is和==的区别
- Python中__init__和__new__的区别详解
- python字符串加颜色区别
- flink实战开发之Join和coGroup的区别和应用
- python 与java中的小区别
- Python7:from module import * 和 import module 的区别
- 在python中的get与post区别