94

假设我想要 10 行数据,但我想要为每行或每条数据增加一个值。如何增加该值?

例如....如果我有这些行,是否有正则表达式替换 id 值以增加?

<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />

--- 这就是我希望它看起来的样子......(如果第一行的 id 上升一个没关系)

<row id="1" />
<row id="2" />
<row id="3" />
<row id="4" />
<row id="5" />
4

8 回答 8

168

不确定正则表达式,但有一种方法可以让您在 Notepad++ 中执行此操作,尽管它不是很灵活。

在您提供的示例中,按住Alt并选择要更改的数字列。然后转到Edit->Column Editor并选择Number to Insert出现的窗口中的单选按钮。然后指定您的初始数字和增量,然后单击“确定”。它应该写出递增的数字。

注意:这也适用于该Multi-editing功能(在Ctrl按住键的同时选择多个位置)。

然而,这远不及大多数人认为有用的灵活性。Notepad++ 很棒,但如果你想要一个真正强大的编辑器,可以轻松完成这样的事情,我会说使用 Vim。

于 2011-09-29T20:14:04.410 回答
16

我今天正在寻找相同的功能,但在 Notepad++ 中无法做到这一点。但是,我们有 TextPad 来拯救我们。它对我有用。

在 TextPad 的替换对话框中,打开正则表达式;那么你可以尝试更换

<row id="1"/>

经过

<row id="\i"/>

查看此链接以了解 TextPad 的更多惊人替换功能 - http://sublimetext.userecho.com/topic/106519-generate-a-sequence-of-numbers-increment-replace/

于 2013-02-04T09:22:48.460 回答
11

我在超过 250 行时遇到了同样的问题,我是这样做的:

例如 :

<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />

您将光标放在“1”之后,然后单击alt + shift并使用向下箭头开始下降,直到到达底线现在您看到一组选择单击擦除以同时擦除每行上的数字 1,然后转到Edit -> Column Editor并选择Number to Insert然后1输入初始数字字段并按字段1递增并检查零数字并单击ok

恭喜你做到了:)

于 2016-03-25T12:43:32.127 回答
5

由于真实答案有限,我将分享此解决方法。对于像您的示例这样非常简单的情况,您可以向后执行...

由此

1
2
3
4
5

替换\r\n" />\r\n<row id=",您将获得 90% 的路径

1" />
<row id="2" />
<row id="3" />
<row id="4" />
<row id="5

或者是一种类似的方式,您可以使用 excel/电子表格来破解数据。只需将原始数据拆分为列并根据需要操作值。

|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |

显而易见的东西,但它可能会帮助做奇怪的一次性黑客工作的人节省一些击键。

于 2012-07-12T03:40:54.443 回答
4

http://docs.notepad-plus-plus.org/index.php/Inserting_Variable_Text

Notepad++ 配备了一个 Edit -> Column "Alt+C" Editor,它可以以两种不同的方式处理矩形选择: Coledit.png 在插入的列处插入一些固定文本,包括当前行和当前行之后点(又名插入符号)。最初选择的文本保持不变。如图所示,可以以相同的方式插入一系列线性数字。应提供起始值和增量。左填充零是一个选项,可以以 2、8、10 或 16 为基数输入数字 - 这也是计算值的显示方式,填充基于最大值。

于 2016-04-13T19:05:50.033 回答
3


上面建议的解决方案只有在数据对齐时才有效。
请参阅使用 PythonScript Notepad++ 插件的链接中的解决方案,效果很好!

stackoverflow 查找/替换但增加值

于 2014-03-06T05:12:03.000 回答
3

如果您将值存储在文件input.txt中,则可以通过正则表达式和 foreach 循环使用 Powershell 来执行此操作:

$initialNum=1; $increment=1; $tmp = Get-Content input.txt | foreach {  $n = [regex]::match($_,'id="(\d+)"').groups[1
].value; if ($n) {$_ -replace "$n", ([int32]$initialNum+$increment); $increment=$increment+1;} else {$_}; }

之后,您可以使用 $tmp 将 $tmp 存储在文件中$tmp > result.txt。这不需要数据在列中。

于 2016-11-14T22:14:51.100 回答
1

(张贴以防有人可能使用它)。

我正在寻找一个比 OP 更复杂的问题的解决方案 - 用递增的数字替换每一次出现的数字

例如替换这样的东西:

<row id="1" />
<row id="2" />
<row id="1" />
<row id="3" />
<row id="1" />

这样:

<row id="2" />
<row id="3" />
<row id="2" />
<row id="4" />
<row id="2" />

无法在线找到解决方案,所以我用 groovy 编写了自己的脚本(有点难看,但可以完成工作):

 /**
 * <p> Finds words that matches template and increases them by 1.
 * '_' in word template represents number.
 *
 * <p> E.g. if template equals 'Row="_"', then:
 * ALL Row=0 will be replaced by Row="1"
 * All Row=1 will be replaced by Row="2"
 * <p> Warning - your find template might not work properly if _ is the last character of it
 * etc.
 * <p> Requirments:
 * - Original text does not contain tepmlate string
 * - Only numbers in non-disrupted sequence are incremented and replaced
 * (i.e. from example below, if Row=4 exists in original text, but Row=3 not, than Row=4 will NOT be 
 * replaced by Row=5)
 */
def replace_inc(String text, int startingIndex, String findTemplate) {
    assert findTemplate.contains('_') : 'search template needs to contain "_" placeholder'
    assert !(findTemplate.replaceFirst('_','').contains('_')) : 'only one "_" placeholder is allowed'
    assert !text.contains('_____') : 'input text should not contain "______" (5 underscores)'
    while (true) {
        findString = findTemplate.replace("_",(startingIndex).toString())
        if (!text.contains(findString)) break;
        replaceString = findTemplate.replace("_", "_____"+(++startingIndex).toString())
        text = text.replaceAll(findString, replaceString)
    }
    return text.replaceAll("_____","") // get rid of '_____' character
}

// input
findTemplate = 'Row="_"'
path = /C:\TEMP\working_copy.txt/
startingIndex = 0

// do stuff
f = new File(path)
outText = replace_inc(f.text,startingIndex,findTemplate)
println "Results \n: " + outText
f.withWriter { out -> out.println outText }
println "Results written to $f"
于 2014-05-16T16:39:07.207 回答