记事本 + + 递增替换

假设我希望有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" />
133987 次浏览

Not sure about regex, but there is a way for you to do this in Notepad++, although it isn't very flexible.

In the example that you gave, hold Alt and select the column of numbers that you wish to change. Then go to Edit->Column Editor and select the Number to Insert radio button in the window that appears. Then specify your initial number and increment, and hit OK. It should write out the incremented numbers.

Note: this also works with the Multi-editing feature (selecting several locations while maintaining Ctrl key pressed).

This is, however, not anywhere near the flexibility that most people would find useful. Notepad++ is great, but if you want a truly powerful editor that can do things like this with ease, I'd say use Vim.

Since there are limited real answers I'll share this workaround. For really simple cases like your example you do it backwards...

From this

1
2
3
4
5

Replace \r\n with " />\r\n<row id=" and you'll get 90% of the way there

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

Or is a similar fashion you can hack about data with excel/spreadsheet. Just split your original data into columns and manipulate values as you require.

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

Obvious stuff but it may help someone doing the odd one-off hack job to save a few key strokes.

I was looking for the same feature today but couldn't do this in Notepad++. However, we have TextPad to our rescue. It worked for me.

In TextPad's replace dialog, turn on regex; then you could try replacing

<row id="1"/>

by

<row id="\i"/>

Have a look at this link for further amazing replace features of TextPad - http://sublimetext.userecho.com/topic/106519-generate-a-sequence-of-numbers-increment-replace/


Solutions suggested above will work only if data is aligned..
See solution in the link using PythonScript Notepad++ plugin, It Works great!

stackoverflow Find/Replace but Increment Value

(Posting in case someone might have a use of it).

I was looking for a solution for a problem a bit more sophisticated than OP - replacing EVERY occurrence of something with the number by same thing with incremented number

E.g. Replacing something like this:

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

By this:

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

Couldnt find the solution online so I wrote my own script in groovy (a bit ugly but does the job):

 /**
* <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"

i had the same problem with more than 250 lines and here is how i did it:

for example :

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

you put the cursor just after the "1" and you click on alt + shift and start descending with down arrow until your reach the bottom line now you see a group of selections click on erase to erase the number 1 on each line simultaneously and go to Edit -> Column Editor and select Number to Insert then put 1 in initial number field and 1 in incremented by field and check zero numbers and click ok

Congratulations you did it :)

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

Notepad++ comes equipped with a Edit -> Column "Alt+C" Editor which can work on a rectangular selection in two different ways: Coledit.png inserting some fixed text on every line including and following the current line, at the column of the insertion point (aka caret). Initially selected text is left untouched. As the picture illustrates, a linear series of numbers can be inserted in the same manner. The starting value and increment are to be provided. Left padding with zeroes is an option, and the number may be entered in base 2, 8, 10 or 16 - this is how the computed values will be displayed too, padding being based on the largest.

You can do it using Powershell through regex and foreach loop, if you store your values in file 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 {$_}; }

After that you can store $tmp in file using $tmp > result.txt. This doesn't need data to be in columns.

You can use this very similar script in Python. It is a very good code. Just change $item_id = abc; with your <row id="abc" /> (in your case)

import os
import re
 

def read_text_from_file(file_path):
"""
Aceasta functie returneaza continutul unui fisier.
file_path: calea catre fisierul din care vrei sa citesti
"""
with open(file_path, encoding='utf8') as f:
text = f.read()
return text
 

 

def write_to_file(text, file_path):
"""
Aceasta functie scrie un text intr-un fisier.
text: textul pe care vrei sa il scrii
file_path: calea catre fisierul in care vrei sa scrii
"""
with open(file_path, 'wb') as f:
f.write(text.encode('utf8', 'ignore'))
 

def incrementare_fisiere_html(cale_folder_html):
"""
Functia itereaza printr-un folder care contine fisiere html si adauga index-ul corespunzator in fiecare fisier
"""
count = 0
current_id = 1
for f in os.listdir(cale_folder_html):
if f.endswith('.html'):
cale_fisier_html = cale_folder_html + "\\" + f
html_text = read_text_from_file(cale_fisier_html)
item_id_pattern = re.compile('\$item_id = (.*?);')
item_id = re.findall(item_id_pattern, html_text)
if len(item_id) > 0:
print("{} a fost modificat. ".format(f))
item_id = item_id[0]
html_text = html_text.replace(item_id, str(current_id))
current_id += 1
count += 1
write_to_file(html_text, cale_fisier_html)
else:
# print("{} nu are $item_id.".format(f))
continue
else:
continue
print("Numarul de fisiere modificate: ", count)
 

if __name__ == '__main__':
# sa pui calea catre folderul cu fisiere
incrementare_fisiere_html('e:\\Folder1')

The explanation and SOURCE code CLICK HERE: