2011-09-29 212 views
68

可以说我想要有10行数据,但是我想为每行或每条数据增加一个值。我如何增加这个价值?记事本++逐渐替换

例如....如果我有这些行,是否有一个正则表达式的方式来代替id值增加?

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

---这是我想它看起来像......(如果第一行的ID上升一个那OK)

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

Notepad ++替代品不做数学,也不做正则表达式。 – Amber

+0

如果有可能,这是非常困难的。在正则表达式中没有数字概念(所以没有+1),并且在匹配正则表达式时没有简单的方法来累积数据(所以没有“下一个”匹配)。如果您真的需要自动执行此操作,XSLT可能会帮助您。 – 2011-09-29 20:12:41

+0

@Amber Notepad ++具有有限的正则表达式功能。 – Karolis

回答

135

不知道有关正则表达式是什么,但有一种让你在Notepad ++中做到这一点的方法,虽然它不是非常灵活。

在您给出的示例中,按住Alt并选择您希望更改的数字列。然后转到Edit->Column Editor并在出现的窗口中选择Number to Insert单选按钮。然后指定您的初始编号和增量,然后按确定。它应该写出增加的数字。

注意:这也适用于Multi-editing功能(选择几个位置,同时保持Ctrl键按下)。

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

+4

+1这两个为晦涩的列功能,并指出vim是一个更强大的编辑器。 ;) –

+0

Notepad ++非常棒,如果我们有一个像这样强大的在线文本编辑器,那将会很棒。 –

+1

对我来说:选择与Alt + Shift列,然后做数字插入。完美的作品 – Rombus

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/spreadsheet来破解数据。只需将您的原始数据拆分为列并根据需要操作值即可。

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

明显的东西,但它可以帮助某人做奇怪的一次性黑客工作,以节省几个关键笔画。

+3

用''在不带'的正则表达式模式下替换'(。*)'。匹配换行符会让你100%的方式。 – satibel

12

我今天正在寻找相同的功能,但无法在Notepad ++中执行此操作。不过,我们有TextPad来拯救我们。它为我工作。

在TextPad的替换对话框中,打开regex;那么你可以尝试通过

<row id="\i"/> 

更换

<row id="1"/> 

看一看这个链接,进一步惊人更换TextPad的特点 - http://sublimetext.userecho.com/topic/106519-generate-a-sequence-of-numbers-increment-replace/

+1

如果我真的注意,我想它会有所帮助。结束安装SublimeText。 :) 谢谢。 –

+0

为我完美工作谢谢! –

+0

但请确保您点击“全部替换”按钮。 “替换下一个”不会增加计数器。花了几个小时才弄明白这一点。 \ i用从1开始的数字替换,递增1. \ i(10)用从10开始的数字替换,递增1. \ i(0,10)替换为从0开始的数字,递增10 \ i(100,-10)用从100开始的数字替换,递减-10。 –

2

(万一有人发帖可能有一个使用它)。

我一直在寻找一个问题略高于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" 
3

我有超过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

恭喜你成功了:)

3

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

记事本++配备了一个编辑 - >列“ALT + C”编辑器可以在两种不同的方式在矩形选择工作:Coledit.png 插入在每一行包括一些固定的文本和之后的电流线,插入点(又名脱字符)的列。最初选定的文本保持不变。 如图所示,可以以相同的方式插入一系列线性数字。提供起始值和增量。可以用零填充左边的填充项,并且可以以2,8,10或16为底数输入数字 - 这也是计算值显示的方式,填充是基于最大值。

+0

如果您在帖子中附加图片以避免混淆读取您的答案会好得多 –

1

您可以通过正则表达式和foreach循环使用PowerShell做到这一点,如果你在文件存储你的价值观input.txt中

$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 > result.txt文件$ TMP。这不需要数据在列中。