微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

findall 并替换包括标记的实例

如何解决findall 并替换包括标记的实例

我使用的是 Python 3.7.9 并且我有某种 HTML 代码,其中包含来自 Pandas 表的一些数据。我想为熊猫表中的特定数据着色,因此我想重新使用字符串标记间的文本并将其替换为其他一些标记(它们在 Confluence 中用于以特定颜色标记文本。)

我的输入文本字符串是:

text = 'some text Now important information starts decrease-123456decrease more text not to touch next marker increase7896278689increase and more text another marker decrease-12355decrease with important information'

替换字符串为:

increase = '<span style=\"color: Red;\">'+val+'</span>'
decrease = '<span style=\"color: Green;\">'+val+'</span>'

和 val 是要在标记之间找到的信息。

所以我的预期输出是:

output = some text Now important information starts <span style=\"color: Green;\">-123456</span> more text not to touch next marker <span style=\"color: Red;\">7896278689</span> and more text another marker <span style="color: Green;">-12355</span> with important information

这是我尝试过的:

import re

text = 'some text Now important information starts decrease-123456decrease more text not to touch next marker increase7896278689increase and more text another marker decrease-12355decrease with important information'
found_increase = re.findall('increase(.+?)increase',text)
found_decrease = re.findall('decrease(.+?)decrease',text)
output=''
for i,val in enumerate(found_increase):
    output=text.replace('increase'+val+'increase','<span style=\"color: Red;\">'+val+'</span>')
for i,val in enumerate(found_decrease):
    output=text.replace('decrease'+val+'decrease','<span style=\"color: Green;\">'+val+'</span>')
print(output)

我也尝试过 Pandas 附带的样式方法,但 Confluence 不是真正的 HTML,因此这种方法对我不起作用。在我上面的例子中,我得到以下输出

Some text Now important information starts decrease-123456decrease more text not to touch next marker increase7896278689increase and more text another marker <span style="color: Green;">-12355</span> with important information

解决方法

我发现此代码有效:

print(re.sub(r"decrease(.*?)decrease",r"<span style=\"color: Green;\">\1</span>",test))

这里发生的事情是我们正在替换模式

"decrease(.*?)decrease" 

"<span style=\"color: Green;\">\1</span>"

其中 \1(.*?) 的内容。注意字符串前的前导 r。您可以了解为什么会出现这种情况here

显然,您也需要为增加版本重新创建它。

请注意 replace() will replace all occurences,您的代码似乎没有考虑到这一点。

,

python 正则表达式引擎直接支持通过捕获组和 re.sub/re.Pattern.sub 进行替换。默认是替换所有出现的模式。

https://docs.python.org/3/library/re.html#re.sub

访问第一个捕获组的模式分别为 r'\1''\\1'

import re
text = 'some text now important information starts decrease-123456decrease more text not to touch next marker increase7896278689increase and more text another marker decrease-12355decrease with important information'
inc_replaced = re.sub('increase(.+?)increase','<span style=\"color: Red;\">\\1</span>',text)
output = re.sub('decrease(.+?)decrease','<span style=\"color: Green;\">\\1</span>',text)

>>> output                                                                                                                                                                                                                                
'some text now important information starts <span style="color: Green;">-123456</span> more text not to touch next marker increase7896278689increase and more text another marker <span style="color: Green;">-12355</span> with important information'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?