【问题标题】:Replace text incrementally on a line by line basis逐行增量替换文本
【发布时间】:2019-06-04 08:00:46
【问题描述】:

逐行替换文本(每行重新开始计数)。

我尝试了各种解决方案,例如 Notepad++ 列替换、TextPad 增量替换和此处列出的 Python 脚本Notepad++ incrementally replace,但没有一个按照我想要的方式工作(它们不会在整行上替换,也不会从每一行重新开始)。

我已经用查找文本替换,但不知道如何增加它。

FIND: (\[2s\])
REPLACE: <br /><b class="num">\1</b>

所以我想要这个:

test text various [2s] tes text various  [2s] tes text various [2s] to replace w abc-mine1.txt , [2s] to replace w abc-mine1.txt , [2s] to replace w abc-mine1.txt , [2s] to replace w
test text various [2s] tes text various  [2s] tes text various [2s]
test text various [2s]

变成这样

test text various <br /><b class="num">1</b> tes text various  <br /><b class="num">2</b> tes text various <br /><b class="num">3</b> to replace w abc-mine1.txt , <br /><b class="num">4</b>] to replace w abc-mine1.txt , <br /><b class="num">5</b> to replace w abc-mine1.txt , <br /><b class="num">6</b> to replace w
test text various <br /><b class="num">1</b> tes text various  <br /><b class="num">2</b> tes text various <br /><b class="num">3</b>
test text various <br /><b class="num">1</b>

【问题讨论】:

  • 无法使其适应我的情况。我使用的没有效果:def increment_after_openparen(match): return "(2s".format(str(int(match.group(1))+1)) editor.rereplace(r'\((\d+)', increment_after_openparen) 引用示例中的增量是基于列应用的,而不是我需要的基于行的。

标签: regex replace notepad++


【解决方案1】:

我认为您无法使用普通的正则表达式查找和替换来做到这一点。

但如果您可以安装 Python 脚本插件,那么下面的代码应该可以解决问题:

repstr = "[2s]"
lcount = editor.getLineCount()  
newstr = ""
i = 0
while i < lcount:
    actline = editor.getLine(i)
    counter = 1
    while actline.find(repstr) > 0:
        actline = actline.replace(repstr, "<br /><b class=\"num\">"+str(counter)+"</b>",1)
        counter += 1
    newstr = newstr + actline
    i += 1

editor.clearAll();
editor.appendText(newstr);

【讨论】:

  • 是的,就是这样。在那之前我找到了一个低技术的解决方案(用选项卡替换所有变量实例,粘贴到 Excel,用增量数字填充列之间,合并列,但下次肯定会使用这个脚本:)
猜你喜欢
  • 2010-10-16
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2017-08-01
  • 2011-11-28
  • 1970-01-01
  • 2023-04-04
  • 2010-10-19
相关资源
最近更新 更多