Vim 用随机生成的数字替换正则表达式 [英] Vim substitute a Regex with randomly generated numbers

查看:37
本文介绍了Vim 用随机生成的数字替换正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用 Vim 中随机生成的数字替换正则表达式?对于匹配正则表达式的每个模式,要替换的(随机)数字应该不同.这是我需要的示例.

Is it possible to substitute a regular expression with a randomly generated number in Vim ? The (random) number to be replaced should be different for each pattern that matches the regular expression. Here's an example of what I need.

输入文件:

<a>XYZ</a>
<a>XYZ</a>
<a>XYZ</a>
<a>XYZ</a>

用随机数替换 XYZ 后,输出可能是:

After substituting XYZ with random numbers, the output could be:

<a>599</a>  
<a>14253</a>    
<a>1718</a>
<a>3064</a>

推荐答案

如果你不介意在你的 vim 中使用一点 perl,你可以使用

If you don't mind a little perl in your vim, you can use

:%! perl -pne 's/XYZ/int(rand 1000)/ge'

根据@hobbes3 的建议,更新为允许在给定的行上进行无限制的替换,所以

updated to allow unlimited substitutions on a given line, per suggestion by @hobbes3, so

XYZ XYZ
XYZ XYZ XYZ 
XYZ XYZ XYZ XYZ XYZ XYZ
XYZ XYZ

变得像

86 988
677 477 394 
199 821 193 649 502 471
732 208

这篇关于Vim 用随机生成的数字替换正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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