爬虫:Re库的match对象

版权声明:关注微信公众号:摸鱼科技资讯,联系我们 https://blog.csdn.net/qq_36949176/article/details/84450881

                                                              Match对象的属性

属性 说明
.string 待匹配的文本
.re 匹配时使用的pattern对象(正则表达式)
.pos 正则表达式搜索文本的开始位置(第几个位置,一般为0)
.endpos 正则表达式搜索文本的结束位置(最后一个位置,即所处的第几个字符)
>>> import re
>>> ls=re.search(r'[1-9]\d{5}','12345678')
>>> if ls:
	print(ls.group(0))

123456
>>> ls.pos
0          //开始位置为第0个字符开始,这里的1为0
>>> ls.endpos
8        //数字8后面一个位置就是结束位置,因为从0开始数,8后面一个位置的角标为8
>>> ls.re
re.compile('[1-9]\\d{5}')
>>> ls.string
'12345678'

                                                                Match 对象的方法

方法 说明
.group(0) 获得匹配后的字符串
.start() 匹配字符串在原始字符串的开始位置
.end() 匹配字符串在原始字符串的结束位置
.span() 返回(.start(),.end())
>>> import re
>>> m=re.search(r'[1-9]\d{5}','BIT100081 tus100084')
>>> m.string
'BIT100081 tus100084'
>>> m.re
re.compile('[1-9]\\d{5}')
>>> m.pos
0
>>> m.endpos
19
>>> m.group(0)
'100081'
>>> m.start()
3
>>> m.end()
9
>>> m.span()
(3, 9)

猜你喜欢

转载自blog.csdn.net/qq_36949176/article/details/84450881