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 回答
![?](http://img1.sycdn.imooc.com/5333a1920001d36402200220-100-100.jpg)
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的结果。
![?](http://img1.sycdn.imooc.com/54586453000163bd02200220-100-100.jpg)
慕姐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
添加回答
举报
0/150
提交
取消