[英]Python regex replace text starting and ending with specific phrases
I want to replace text starting with <img>
and ending with </img>
.我想替换以<img>
开头并以</img>
结尾的文本。 Im just starting with regex stuff.我只是从正则表达式开始。
I have tried below code.我试过下面的代码。
Final result should be:最终结果应该是:
input:输入:
"New Year's Eve <img>scr=[...]</img>, New Year's Day (Observed)"
output:输出:
"New Year's Eve [image placeholder], New Year's Day (Observed)"
Code sample代码示例
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))`
You want to replace all the code between the opening and closing tag:您想替换开始和结束标记之间的所有代码:
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'<img>.*?</img>', '[image placeholder]', holiday))
Output:输出:
Independence Day (Observed)
Another Christmas Eve, Christmas Day (Observed)
New Year's Eve [image placeholder], New Year's Day (Observed)
Martin Luther King, Jr. [image placeholder] Day
The regex matches text starting with <img>
and ending with </img>
with anything in between .*?
正则表达式匹配以<img>
开头并以</img>
结尾的文本以及介于.*?
之间的任何内容.*?
. . ?
makes it match non greedily, so that it will match until the first closing tag that follows the opening tag - that allows it to replace every pair of tags correctly if there are several in the same input text.使其非贪婪地匹配,以便它匹配直到开始标记之后的第一个结束标记 - 如果同一输入文本中有多个标记,则允许它正确替换每对标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.