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()
.
精彩评论