开发者

Add constant value to numeric XML attribute

开发者 https://www.devze.com 2023-02-04 23:46 出处:网络
Background Add a constant value to numbers matched with a regular expression, using vim (gvim).开发者_StackOverflow中文版

Background

Add a constant value to numbers matched with a regular expression, using vim (gvim).开发者_StackOverflow中文版

Problem

The following regular expression will match width="32":

/width="\([0-9]\{2\}\)"

Question

How do you replace the numeric value of the width attribute with the results from a mathematical expression that uses the attribute's value? For example, I would like to perform the following global replacement:

:%s/width="\([0-9]\{2\}\)"/width="\1+10"/g

That would produce width="42" for width="32" and width="105" for width="95".

Update

Looks like this cannot be done using regex alone -- is there a vim-way?

Thank you!


%s/width="\zs\d\{2}\ze"/\=submatch(0)+10/g

See documentation for /\zs, /\ze, sub-replace-expression and submatch().

0

精彩评论

暂无评论...
验证码 换一张
取 消