I want to to this:
<div><img id="u115_img" class="img " src="images/u45.png"/></div>
<div id='u46'><img id="u115_img" class="img " src="images/u46.png"/></div>
<span><img id="u115_img" class="img " src="images/u47.png"/></span>
to
<div><img id="u115_img" class="img " src="cid:images/u45.png"/></div>
<div id='u46'><img id="u115_img" class="img " src="cid:images/u46.png"/></div>
<span><img id="u115_img" class="img " src="cid:images/u47.png"/></span>
and I need to return:
images/u45.png
images/u46.png
images/u47.png
so I do this as follow:
img_src_reg_1 = re.compile(ur'<img[^>]*src\s*=\s*"([^"]*)')
img_src_reg_2 = re.compile(ur'(<img[^>]*src\s*=\s*")([^"]*)')
# find the img src
for img_url in img_src_reg_1.findall(content):
fp = open(u"../static/{}".format(img_url))
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', u'<{}>'.format(img_url))
msg.attach(img)
# change string
txt = img_src_reg_2.sub(r"\1cid:\2", content)
msg_txt = MIMEText(txt.encode('utf-8'), 'html')
msg.attach(msg_txt)
I want to know can I change the two regex into one
? And also, any good suggestion to simplify the codes?