用于生成html标记的两个组

2024-07-07 08:53:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要你帮我学正则表达式

https://regex101.com/r/r3pTh0/2

我可以使用以下正则表达式,但仍然需要帮助:

\<\%= image_tag image_url\(\“(.+?)\”\)|\“(.+?)\” %>

替换为:

<img src="\1" alt="\2">

python代码:

originalData = re.sub('<%= image_tag image_url("(.+?)")+, :alt => "(.+?)\" %>', r'<img src="\1" alt="\2">', originalData, flags=re.MULTILINE)

但它似乎并没有取代任何东西

有一个字符串:

<%= image_tag image_url(“/blog/assets/images/2018-11-15/dribbble-developer-interview-jeffrey-chupp.png”), :alt => “Developer interview with Jeffrey Chupp, Director of Engineering at Dribbble” %>

将其替换为html img标记:

<img src="/blog/assets/images/2018-11-15/dribbble-developer-interview-jeffrey-chupp.png" alt="Developer interview with Jeffrey Chupp, Director of Engineering at Dribbble">
  1. 在图片链接的开头添加http://somesite.com是否也很困难

Tags: imageresrccomurldeveloperimgtag
1条回答
网友
1楼 · 发布于 2024-07-07 08:53:19

您遇到此错误是因为您在regex101.com上选择了PHP风格的regex

只需切换到Python风格,网站就会为您修复正则表达式。命令应为:

originalData = re.sub(r"\<\%= image_tag image_url\(\“(.+?)\”\)+\, :alt => “(.+?)\” %>", r'<img src="\1" alt="\2">', originalData, flags=re.MULTILINE)

至于向映像src添加域前缀,只需在第二个regex过程中将src="替换为src="https://somesite.com

或者您可以在替换字符串前面添加域,如下'<img src="https://somesite.com\1" alt="https://somesite.com\2">'

相关问题 更多 >