python - 如何迭代多个正则表达式匹配并替换它们?
问题描述
将包含单词恐惧的每个句子替换为相同的句子,用 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 标记中。
解决方案
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>.
推荐阅读
- jenkins - 如果 Jenkins 主/节点在 EC2 现货实例中失败或停止会发生什么?
- python - Pandas:定义 value_count() int 时选择第一次出现
- javascript - MarkLogic Node.js。如何在结果中指定返回属性
- list - Haskell检查函数是否两次返回相同的值
- database - 如果没有互联网连接,我们如何处理用户的输入
- java - 使用 Executor(新线程)将 Master/Detail Flow 应用程序连接到服务器
- groovy - 有没有办法配置允许哪些不安全的 Groovy 操作?
- plasticscm - Plastic Cloud 版本中是否有持续集成的方法?
- python - 在 aws ElasticBean Stalk 服务器上使用 os.getcwd() 读取文件时出现问题
- json - 如何在flutter中解析复杂的json数据并分配给模型?