我有类似下面的变量:蟒蛇正则表达式查找图片路径
var = '<img src="path_1"><p>Words</p><img src="path_2>'
它是一个字符串,但里面显然是HTML元素。我如何才能使用正则表达式获得第一条路径(即path_1)?
我想是这样的:
match = re.match(r'src=\"[\w-]+\"', var)
print match.group(0)
我得到这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
任何帮助表示赞赏。
尝试're.search()' – thebjorn
'match'只能从开头找到.. ['如果字符串开头的零个或多个字符匹配正则表达式模式](https://docs.python.org /2/library/re.html#re.match) – rock321987