我有類似下面的變量:蟒蛇正則表達式查找圖片路徑
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