Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

python - 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'<img>', '[image placeholder]', holiday))`
  ask by AstroLeh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(使其非贪婪地匹配,以便匹配,直到在开始标记之后的第一个结束标记为止-如果在同一输入文本中有多个标记,则它可以正确替换每对标记。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...