python - 提取图片文件名,并替换<img>标签
怪我咯
怪我咯 2017-04-18 09:53:20
0
1
411

本功能类似于将<img>标签替换成markdown格式的文本,要提取其中的文件名,并替换<img>标签。
如:

图片:<font color="#000000"><img width="147" height="52" src="http://127.0.0.1:80/admin/../upload/pimg1054_1.png" alt="" /></font></p> <img width="147" height="52" src="admin/../upload/asffas_ad.jpg" alt="" /><p style="margin-bottom: 0cm;">保留两位小数</p> </p>

替换结果为:

图片:<font color="#000000">pimg1054_1.png</font></p> asffas_ad.jpg<p style="margin-bottom: 0cm;">保留两位小数</p> </p>

我写的正则:

import re

a = '图片:<font color="#000000"><img width="147" height="52" src="http://127.0.0.1:80/JudgeOnline/admin/../upload/pimg1054_1.png" alt="" /></font></p> <img width="147" height="52" src="http://127.0.0.1:80/JudgeOnline/admin/../upload/asffas_ad.png" alt="" /><p style="margin-bottom: 0cm;">保留两位小数</p> </p>'
#re_img = re.compile('<\s*img.*src=[\"\']?.*?(\/\.[jpngif]+)[\"\']?.*\/>')
re_img = re.compile('''<\s*img.*src=["']?[\S]+\/([^\/]+\.[jpngif]+)["']?.*?>''')
s = re_img.sub(r'\1', a)

print(s)

貌似匹配了第一图片标签的<img和最后一个图片标签的/>,结果只保留了一个图片。
请问该怎么替换?

怪我咯
怪我咯

走同样的路,发现不同的人生

répondre à tous(1)
迷茫
import re

reg='<img.*?src="(.*?\.(jpg|png))".*?/>'
content='图片:<font color="#000000"><img width="147" height="52" src="http://127.0.0.1:80/admin/../upload/pimg1054_1.png" alt="" /></font></p> <img width="147" height="52" src="admin/../upload/asffas_ad.jpg" alt="" /><p style="margin-bottom: 0cm;">保留两位小数</p> </p>'
m=re.search(reg,content)
imgName=m.group(1).split('/')[-1]
res=re.sub(reg,imgName.strip(),content)
print(res)

Est-ce vrai ?

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!