python - 如何迭代多个正则表达式匹配并替换它们?

标签 python html regex

将包含“fear”一词的每个句子替换为同一个句子,并用 class="fear"包裹在 b 标签中。

尝试将此模式的每个(总共 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 标记中。

最佳答案

您可以使用替换来迭代 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)

文件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>.<b class="fear"> Any genes endowing a chicken with 
this selective fear, reducing the time spent in futile and costly combat, should flourish</b>.

关于python - 如何迭代多个正则表达式匹配并替换它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65084880/

相关文章:

Python 正则表达式 : Can't extract message containing escaped quotes

java - 正则表达式将以下行拆分为各个字段?

python - 根据 pearsonr 值 : 更改每个 PairGrid Seaborn 图中的字体颜色

python - 给定一个列表字典,是否有一种 pythonic 智能方法来比较每个列表的第 i 个元素并提取最大值?

python - 膳食计划算法?

javascript - 如何在服务器处理时禁用所有内容、div 等

javascript - Html5 地理位置clearWatch() 没有响应

python - 改变html中flask变量的值

javascript - javascript 正则表达式出现问题并替换

python - 以最Pythonic的方式从字典列表生成配置样式文件