简体   繁体   English

Python正则表达式替换以特定短语开头和结尾的文本

[英]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.

相关问题 将具有所需值的字符串中的&#39;&lt;&lt;&#39;替换为开始,将&#39;&#39;&gt;&#39;`替换为结束短语 - Replace `'<'` as starting and `'>'` as ending phrases in a string with wanted value 使用Python正则表达式查找以特定字母开头和结尾的单词 - Using Python regex find words starting and ending with specific letters 通过使用正则表达式加上字典或python中的哈希映射来动态替换句子中单词的所有开头和结尾字母 - Dynamically replace all starting and ending letters of words in a sentence by using regex plus a dictionary or Hash map in python 如何匹配使用正则表达式来匹配具有特定开始和结束模式的多行文本 - How do I match use regex to match multi-line text with specific starting and ending patterns Python 正则表达式 - 获取 Substring 以部分单词开头并以某个字符结尾 - Python Regex - Get Substring Starting with part of word and ending with certain character Python:在Python中查找并删除以特定子字符串开头和结尾的字符串 - Python: Find and remove a string starting and ending with a specific substring in python Python替换文本(正则表达式?) - Python replace text (regex?) 如何使用开始和结束条件从文本中读取特定行? - How to read specific lines from text using a starting and ending condition? 通过RegEx在Python数据框中删除各种文本短语 - Removing varying text phrases through RegEx in a Python Data frame 在Python中无需RegEx即可从文本文件替换或删除特定符号 - Replace or Delete specific symbols from text file without RegEx in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM