0%

python_Re模块的使用

python_Re模块的使用:

# findall: 匹配字符串中所有的符合正则的内容  返回一个列表
lst = re.findall(r"\d+", "我的电话是:10086,你妈妈的电话是:10010")
prind(lst)

# 结果 ['10086', '10010']


# finditer: 匹配字符串中的所有内容 (返回一个迭代器) 从迭代器中拿到内容 需要.group()
it = re.finditer(r"\d+", "我的电话是:10086,你妈妈的电话是:10010")
for i in it:
print(i.group()) # 从迭代器中拿数据


# search: 全文匹配正则数据,返回第一个匹配到的数据 (返回对象match) 拿数据 需要.group()
sea = re.search(r"\d+", "我的电话是:10086,你妈妈的电话是:10010")
sea.group()
# 结果 '10086'


# match: 从开始匹配数据 (返回对象match) 拿数据 需要.group()
sea = re.match(r"\d+", "我的电话是:10086,你妈妈的电话是:10010")
sea.group()
# 结果 无法匹配到

sea = re.search(r"(?P<call>\d+)", "我的电话是:10086,你妈妈的电话是:10010")

预加载正则表达式:

obj = re.compile(r"\d+")
res = obj.findall("我的电话是:10086,你妈妈的电话是:10010")
# 结果: ['10086','']

img