一:简介
re模块为正则提供了无尽的可能,也是爬虫,数据处理必备利器!
二:主要用法
1: re.match
# re.match(pattern, string, flags)
print('re.match(pattern, string, flags)-----------------------------------------')
string = 'Cats are smarter than dogs'
r = re.match(r'(c.*) are (.*?) than (.*s)', string, re.M | re.I)
try:
"""r cannot be None, otherwise an error will be reported."""
print(r)
print(r.span())
print(r.group().__class__)
print(r.group(1))
print(r.group(2))
print(r.group(3))
# print(r.group(4))
except BaseException as e:
print(e)
2: re.search
# re.search(pattern, string, flags)
print('re.search(pattern, string, flags)----------------------------------------')
string = 'Cats are smarter than dogs'
r = re.sea