正则表达式自动增量替换
正则表达式中是否有一个字符串可以指示它自动递增它的替换,无论它们是数字还是字母。
谢谢您
例如,
,我的字符串应该是数字 1, 2, 3, 4, 5
但它们目前编号为 1, 1, 1, 1, 1
我如何将这 5 个单独的相似字符串中的数字替换为 1, 2, 3, 4, 5
is there a string in regular expressions that can instruct it to autoincrement it's replacements, whether they be numbers or letters.
Thank you
for instance, I have strings that should be number 1, 2, 3, 4, 5
but they are currently numbered as 1, 1, 1, 1, 1
how would I replace the number in those 5 individual similar strings to be 1, 2, 3, 4, 5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 TextWrangler 的语法;但是,它使用
pcre
因此,只要您有办法为递增变量分配初始值(在本例中,我使用$ii)...下面的脚本将任何出现的“pizza-x”替换为“pizza-0”、“pizza-1”...
结果...
魔法来自
s///e;< /code> 和
$ii++
;确保将非递增字符串括在引号中并与句点连接。或者,只需使用 perl -pi -e '$ii = 0; 进行自动增量修改即可。 s/something/"here".$ii++/e ` 直接在文本文件上(当然,首先制作备份副本)。
I'm not familiar with the syntax of TextWrangler; however, it uses
pcre
so this should be what you want as long as you have a way to assign an initial value to your incrementing variable (in this case, I use$ii
)... the script below replaces any occurrence of "pizza-x" with "pizza-0", "pizza-1"...Results...
The magic comes from
s///e;
and$ii++
; be sure you enclose the non-incrementing string in quotes and concatenate with a period.Alternatively, just do your auto-increment mangling with
perl -pi -e '$ii = 0; s/something/"here".$ii++/e
` directly on the text file (make a backup copy first, of course).