【Python3 爬虫】U13_正则表达式之group分组

目录

1.group分组

在正则表达式中,可以对过滤的字符串进行分组,分组使用圆括号的方式。
1.group()group(0)是等价的
2.groups返回的是里面的子组,索引从1开始
3.group(1):返回的是第一个子组,可以传如多个

import re
text = 'The price A is $33,price B is $100'
ret = re.search(r".*(\$\d+).*(\$\d+)",text)
print(ret.group()) # The price A is $33,price B is $100
print(ret.group(0)) # The price A is $33,price B is $100
print(ret.group(1)) # $33
print(ret.group(2)) # $100
print(ret.group(1,2)) # ('$33', '$100')
print(ret.groups()) # ('$33', '$100')

猜你喜欢

转载自www.cnblogs.com/OliverQin/p/12618725.html
今日推荐