首页 > 解决方案 > 如何迭代多个正则表达式匹配并替换它们?

问题描述

将包含单词恐惧的每个句子替换为相同的句子,用 class="fear" 包裹在 ab 标记中。

尝试将此模式的每个(共 2 个)匹配项包装在 html 标记中。

import re
with open('chicken.txt', 'r') as file:
pattern = re.compile(r'[^\.]+fear[^\.]+')
text = file.read()
matches = pattern.finditer(text)
tagstart = '<b class="fear">'
tagend = '</b>'

replacement = [text.replace(match[0], tagstart + match[0] + tagend) for match in matches]

with open('chick.html', 'w') as htmlfile:
    htmlfile.write(replacement[0])

chick.html 输出如下所示:

If you've spent much time with chickens, you may doubt their ability to process a thought as complex as
"Chicken A will beat me anyway, so why bother to fight?" Your doubt is well placed.
Pecking orders are yet another case where the "thinking" has been done by natural selection,
and so needn't be done by the organism.<b class="fear"> The organism must be able to tell its neighbors apart,
and to feel a healthy fear of the ones that have brutalized it, but it needn't grasp the logic behind the fear</b>.
Any genes endowing a chicken with this selective fear, reducing the time spent in futile and costly combat, should flourish.

最后一句是替换变量中的第二项,并且 in 没有包含在那个 b 标记中。

标签: pythonhtmlregex

解决方案


match您可以使用 replace 对每个进行迭代,findinter但每次都对整个文本执行替换。

import re
pattern = re.compile(r'[^\.]+fear[^\.]+')
tagstart = '<b class="fear">'
tagend = '</b>'

with open('chicken.txt', 'r') as file:
    text = file.read()
    matches = pattern.finditer(text)

    for match in matches:
        text = text.replace(match[0], tagstart + match[0] + tagend)

with open('chick.html', 'w') as htmlfile:
    htmlfile.write(text)

文件小鸡.html

If you've spent much time with chickens, you may doubt their ability to process a
 thought as complex as "Chicken A will beat me anyway, so why bother to fight?" Your doubt 
is well placed. Pecking orders are yet another case where the "thinking" has been done by natural 
selection, and so needn't be done by the organism.<b class="fear"> The organism must be able to tell 
its neighbors apart, and to feel a healthy fear of the ones that have brutalized it, but 
it needn't grasp the logic behind the fear</b>.<b class="fear"> Any genes endowing a chicken with 
this selective fear, reducing the time spent in futile and costly combat, should flourish</b>.

推荐阅读