有這樣的文字:如何使用python regexp從字符串中提取圖像名稱?
body = """Some junk texts here.
<img src="/images/15244/somerandomname.jpg" class="news-img">
More texts here"""
我不知道我該怎麼使用Python正則表達式提取somerandomname.jpg
?
我想出了是這樣的:
import re
regex = re.findall('/images/(\d+)/(\w+).jpg', body)
但它返回一個空列表。
我想你確切的代碼,我得到這個'[( '15244', 'somerandomname')]' 嘗試的表達和捕獲前加'r' .jpg也是:'regex = re.findall(r'/ images /(\ d +)/(\ w + \ .jpg)',body)'。 – Morb
同樣在這裏。我用你想要的一個元組獲得了一個列表(除了'somerandomname'而不是'somerandomname.jpg')。 編輯:@Babr你可以告訴你如何執行你的代碼? – gonczor