94

10 行のデータが必要であるが、各行またはデータごとに値を増やしたいとしましょう。その値を増やすにはどうすればよいですか?

たとえば....これらの行がある場合、id 値をインクリメントに置き換える正規表現の方法はありますか?

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

--- これが私が望むものです... (最初の行の ID が 1 つ上がれば問題ありません)

<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次に、初期数と増分を指定して、[OK]をクリックします。インクリメントされた数値を書き出す必要があります。

注:これはこの機能でも機能します(キーを押したMulti-editingまま複数の場所を選択します)。Ctrl

ただし、これは、ほとんどの人が役立つと思われる柔軟性にはほど遠いものです。Notepad ++は素晴らしいですが、このようなことを簡単に実行できる本当に強力なエディターが必要な場合は、Vimを使用すると思います。

于 2011-09-29T20:14:04.410 に答える
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   |   " />    |

明らかなことですが、それは誰かが奇妙な1回限りのハックジョブを実行していくつかのキーストロークを節約するのに役立つ可能性があります。

于 2012-07-12T03:40:54.443 に答える
4

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

Notepad++ には、編集 -> 列 "Alt+C" エディターが装備されており、2 つの異なる方法で四角形の選択範囲を操作できます: 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 > 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 に答える