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

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

问题描述

我有这个代码:

  array('id'=> 1,'name'=& ,'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),
数组('id'=> 1,'name'=>Barss,'date_of_birth'=>2014-05-31 ,breed_id=> 2),
array('id'=> 1,'name'=>Nonam,'date_of_birth'=>2014-05-31, => null

我想要更改所有'id' => 1 (除了第1个),所以数字将增加b y 1.使用Emacs实现很简单:

  Mx replace-regexp 
\(1,\)
\,(1+ \#),

如上所述这里。经过一些研究,我使用Vim实现的最大限度是(从 here ):

 :let i = 1 | g / 1,/ s // \ = i / |让我+ = 1 

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

  array('id'=> 1'name'=>Murka,'date_of_birth'=>2014-10-31, => 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),
数组('id'=> 4'name'=>Meowy,'date_of_birth'=>2014-01-31,breed_id=> 4),
数组('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'=> 'name'=>Nonam,'date_of_birth'=>2014-05-31,breed_id=> null),

所以我必须解决它(我知道这很容易)。 p>

我知道这个和宏我只想知道Vim中是否有单行命令解决方案。



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

  CM-%\(^。* \)\(linear-渐变(\)\(to right\ | to bottom\)\(。* $ \)< RET> 
\& Cq Cj
\1-prefix -\2\,(if(equal)to right\3)lefttop)\4

这个一年前帮助我重构了一些巨大的可怕的HTML代码,内置了很多内联CSS。

解决方案

我没有回答你的一般问题,但是我有一个针对你的具体情况,你可以通过将逗号放在正面的前提下,使你的命令工作,如下所示: p>

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

现在,它只会替换 1


I have this code:

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

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+ \#),

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).

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

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

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

Now it will only replace the 1.

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

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