猿问

python中的正则匹配问题。

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==" />

呢?

守候你守候我
浏览 655回答 2
2回答

BIG阳

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的结果。

慕姐4208626

首先,你不应该使用findall的方法,只是匹配这段文字且匹配一次,直接用search即可。具体可以参考python正则表达式的使用。 其次,你正则也写错了,肯定是匹配不出来你要的那个结果。 改了一下你的代码,这段代码运行结果和你预期一致,但估计实际使用的正则还要根据情况进行调整。 import re str='<input type="hidden" name="__VIEWSTATE" value="dDwyODE2NTM0OTg7Oz5K4/a3o5bbNExGrhnw9E1HvQLfjg==" />' result = re.search('<input [^>]*name=\"__VIEWSTATE\"[^>]*value=\"([^"]*)\"[^>]*>',str) print result
随时随地看视频慕课网APP

相关分类

Python
我要回答