Emacs replace regexp with incremental sequence

大憨熊 提交于 2019-12-08 07:46:38

问题


I am trying to figure out a way to replace a string in a text file by a number that increments by more than 1. I am trying to turn a couple of lines like this:

result_A_in_S1-S2.txt
result_A_in_S1-S2.txt
result_A_in_S1-S2.txt
result_A_in_S1-S2.txt

Into something that scales up sequentially for S1 and S2.

result_A_in_1000-1003.txt
result_A_in_1004-1007.txt
result_A_in_1008-1011.txt
result_A_in_1012-1015.txt

I want to know if I can define a string with replace regexp and then have that string be replaced with some starting number, and as it finds the next occurrence of string, it replaces with starting number + chosen increment.

I am just now starting learn Emacs and am pretty unfamiliar with it.


回答1:


Yes, you can do that with [query-]replace-regexp in Emacs, by evaluating elisp in your replacement, and utilising the zero-based replacement counter \#. e.g.:

M-x query-regexp-replace RET S1-S2 RET
\,(let ((start (+ 1000 (* 4 \#)))) (format "%d-%d" start (+ start 3))) RET



来源:https://stackoverflow.com/questions/49519627/emacs-replace-regexp-with-incremental-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!