为了账号安全,请及时绑定邮箱和手机立即绑定

python中的正则匹配问题。

python中的正则匹配问题。

守候你守候我 2019-02-23 18:28:33
import re str='<input type="hidden" name="__VIEWSTATE" value="dDwyODE2NTM0OTg7Oz5K4/a3o5bbNExGrhnw9E1HvQLfjg==" />' result = re.findall('t[^>]*name=\"__VIEWSTATE\"[^>]*value=\"([^"]*)\"[^>]*>',str) print result 为何输出是 dDwyODE2NTM0OTg7Oz5K4/a3o5bbNExGrhnw9E1HvQLfjg== 而不是 <input type="hidden" name="__VIEWSTATE" value="dDwyODE2NTM0OTg7Oz5K4/a3o5bbNExGrhnw9E1HvQLfjg==" /> 呢?
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

Return all non-overlapping matches of pattern in string, as a list of
strings. The string is scanned left-to-right, and matches are returned
in the order found. If one or more groups are present in the pattern,
return a list of groups
; this will be a list of tuples if the pattern
has more than one group. Empty matches are included in the result
unless they touch the beginning of another match.

我把文档处加黑了,注意你的正则里有capture group,findall() 只返回含有capture group的结果。

查看完整回答
反对 回复 2019-03-01
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

首先,你不应该使用findall的方法,只是匹配这段文字且匹配一次,直接用search即可。具体可以参考python正则表达式的使用
其次,你正则也写错了,肯定是匹配不出来你要的那个结果。

改了一下你的代码,这段代码运行结果和你预期一致,但估计实际使用的正则还要根据情况进行调整。

import re

str='<input type="hidden" name="__VIEWSTATE" value="dDwyODE2NTM0OTg7Oz5K4/a3o5bbNExGrhnw9E1HvQLfjg==" />'

result = re.search('<input [^>]*name=\"__VIEWSTATE\"[^>]*value=\"([^"]*)\"[^>]*>',str)

print result
查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 654 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信