2011-09-29 79 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。這不需要數據在列中。