re模块其实要用到正则表达式,推荐看一下
https://blog.csdn.net/ZYC88888/article/details/98479629
https://www.runoob.com/regexp/regexp-tutorial.html
直接上代码讲解函数:
import re
#compile:预加载,将正则表达式转化为对象
test = re.compile(r"\d+",re.S) # re.S: 让.能匹配换行符
# findall: 返回string中所有与pattern相匹配的全部字符串,得到数组
l1 = test.findall("学号:100111, 密码:285908, 日期20220702")
print("findall",l1)
# finditer: 搜索字符串,返回一个匹配结果的迭代类型,每个迭代元素是match对象, 从迭代器中拿到内容需要.group()
l2 = test.finditer("学号:100111, 密码:285908, 日期20220702")
print("finditer")
for i in l2:
print(i.group())
# search, 扫描整个字符串并返回第一个成功的匹配,若string中包含pattern子串,则返回Match对象,否则返回None,拿数据需要.group()
l3 = test.search("学号:100111, 密码:285908, 日期20220702")
print("search",l3.group())
# match从开始位置开始往后查找,返回第一个符合规则的对象,如果开始位置不符合匹配队形则返回None
l4 = test.match("100111, 密码:285908, 日期20220702")
print("match",l4.group())
可以输出看一下: