【发布时间】:2019-12-01 16:28:24
【问题描述】:
我想替换以<img> 开头并以</img> 结尾的文本。
我只是从正则表达式的东西开始。
我试过下面的代码。
最终结果应该是:
输入:
"New Year's Eve <img>scr=[...]</img>, New Year's Day (Observed)"
输出:
"New Year's Eve [image placeholder], New Year's Day (Observed)"
代码示例
import re
input = [
"Independence Day (Observed)",
"Another Christmas Eve, Christmas Day (Observed)",
"New Year's Eve <img>scr=[...]</img>, New Year's Day (Observed)",
"Martin Luther King, Jr. <img>scr=[...]</img> Day"
]
for holiday in input:
print(re.sub(r'\b<img>\b', '[image placeholder]', holiday))`
【问题讨论】:
-
你确定图片标签是这样的吗?当然,他们更有可能看起来像这样:
<img src="some/path/foo.jpg">...</img>或<img src="some/path/foo.jpg" />。 -
当然,我的错,我重新输入了代码,因为我离开了我的电脑。感谢您指出明显的错字
标签: python regex python-3.x string replace