Vim - 通过正则表达式搜索和替换进行增量编号 [英] Vim - incremental numbering via regular expression search and replace

查看:35
本文介绍了Vim - 通过正则表达式搜索和替换进行增量编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

array ('id' => 1, 'name' => "Murka", 'date_of_birth' => "2014-10-31", "breed_id" => 1),
array ('id' => 1, 'name' => "Jurka", 'date_of_birth' => "2014-11-31", "breed_id" => 2),
array ('id' => 1, 'name' => "Nyash", 'date_of_birth' => "2014-12-31", "breed_id" => 3),
array ('id' => 1, 'name' => "Meowy", 'date_of_birth' => "2014-01-31", "breed_id" => 4),
array ('id' => 1, 'name' => "Yummi", 'date_of_birth' => "2014-10-31", "breed_id" => 2),
array ('id' => 1, 'name' => "Barss", 'date_of_birth' => "2014-05-31", "breed_id" => 2),
array ('id' => 1, 'name' => "Nonam", 'date_of_birth' => "2014-05-31", "breed_id" => null

我想更改所有 'id' =>1(第一个除外)所以数字会增加1.用Emacs很容易实现:

I'll want to change all 'id' => 1 (except the 1st) so the number will increment by 1. It's simple to achieve with Emacs:

M-x replace-regexp
(1,)
,(1+ #),

此处所述.经过一番研究,我使用 Vim 所能达到的最大值是(灵感来自 这里):

As described here. After some research the maximum that I've been able to achieve with Vim is (inspired from here):

:let i=1 | g/1,/ s//=i/ | let i+=1

但这会删除以下所有逗号:

But this removes all the following commas:

array ('id' => 1 'name' => "Murka", 'date_of_birth' => "2014-10-31", "breed_id" => 1),
array ('id' => 2 'name' => "Jurka", 'date_of_birth' => "2014-11-31", "breed_id" => 2),
array ('id' => 3 'name' => "Nyash", 'date_of_birth' => "2014-12-31", "breed_id" => 3),
array ('id' => 4 'name' => "Meowy", 'date_of_birth' => "2014-01-31", "breed_id" => 4),
array ('id' => 5 'name' => "Yummi", 'date_of_birth' => "2014-10-31", "breed_id" => 2),
array ('id' => 6 'name' => "Barss", 'date_of_birth' => "2014-05-31", "breed_id" => 2),
array ('id' => 7 'name' => "Nonam", 'date_of_birth' => "2014-05-31", "breed_id" => null),

所以我必须修复它(我知道这很容易).

So I have to fix it (I know it's easy).

我知道 this 和宏,我只是想知道是否有Vim 中的任何单行命令解决方案.

I am aware of this and macros, I'm just interested to know if there any one-line command solution in Vim.

更一般的问题:是否可以在 Vim 中注入一些逻辑,如条件语句,操纵正则表达式的反向引用?Emacs 中的示例是:

More general question: is it possible in Vim to inject some logic like conditional statements, manipulating on regex back references? The example for this in Emacs would be:

C-M-% (^.*)(linear-gradient()(to right|to bottom)(.*$) <RET>
& C-q C-j
1-prefix-2,(if (equal "to right" 3) "left" "top")4

大约一年多前,这个帮助我重构了一些包含大量内联 CSS 的巨大可怕的 HTML 代码.

This one helped me about a year+ ago to refactor some huge scary HTML code that had a much of inline CSS.

推荐答案

对于您的一般性问题,我没有答案,但对于您的具体情况,我有一个答案.您可以通过将逗号置于正向预测中来使您的命令起作用,如下所示:

I don't have an answer to your general question, but I do have one for your specific situation. You can make your command work by putting the comma into a positive look-ahead, like this:

:let i=1 | g/1(,)@=/ s//=i/ | let i+=1

现在它只会替换1.

这篇关于Vim - 通过正则表达式搜索和替换进行增量编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆