预加载数据
import re
#预加载正则表达式
obj=re.compile(r"\d+")
findall函数
# findall匹配字符串中所有符合正则的数据(返回列表)
rps = obj.findall( " 违法 2321 窝的 64454")
print(rps)
finditer函数
# finditer匹配字符串中所有符合正则的数据(返回迭代器)
psr = obj.finditer( " 违法 2321 窝的 64454")
for i in psr:
# 获取迭代器中数据
print(i.group())
search函数
#仅返回首个数据 search返回match数据,需要 .group()
prs=obj.search(" 违法 2321 窝的 64454")
print(prs.group())
match函数
#match从头匹配,如果不是起始位置匹配成功会返回none
# 如下会发生报错:
prs=obj.match(":2321 窝的 64454")
print(prs.group())
常用正则表达式表示方法
s='''
<input type="属性值(如文本字段,复选框,掩码后的文本控件,单选按钮)"/><br/>
<input type="file"/><br/>
<input type="text" name="user" value="用户名" maxlength="6"/><br/>
<input type="password" name="pasw" maxlength="11"/><br/>
<input type="button" value="获取验证码"/>
<input type="submit" value="注册"/><br/>
<input type="reset" value="重新提交"/><br/>
'''
# 让.匹配换行符
# (?P<命名>.*?) 然后可以group(命名)提取指定内容
obj=re.compile(r'<input type="(?P<sj>.*?)"/>',re.S)
rsp=obj.finditer(s)
for i in rsp:
print(i.group("sj"))