【问题标题】:findall and replace instances including markersfindall 和 replace 实例,包括标记
【发布时间】:2021-02-13 23:05:41
【问题描述】:

我正在使用 Python 3.7.9,并且我有一些 HTML 代码,其中包含来自 pandas 表的一些数据。我想为 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 &lt;span style="color: Green;"&gt;-12355&lt;/span&gt; with important information

【问题讨论】:

标签: python string replace findall confluence-rest-api


【解决方案1】:

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'

【讨论】:

    【解决方案2】:

    我发现这段代码可以代替:

    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,您的代码似乎没有考虑到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2017-02-11
      • 2019-03-12
      相关资源
      最近更新 更多