数学在Vim中搜索替换

时间:2021-06-17 16:49:33

I have a file with times (minutes and seconds), which looks approximately as follows:

我有一个带有时间(分钟和秒)的文件,大致如下:

02:53 rest of line 1...
03:10 rest of line 2...
05:34 rest of line 3...
05:35 rest of line 4...
10:02 rest of line 5...
...

I would like to replace the time by its equivalent in seconds. Ideally, I would like to run some magical command like this:

我想以秒为单位来替换时间。理想情况下,我想运行一些像这样的神奇命令:

:%s/^\(\d\d\):\(\d\d\) \(.*\)/(=\1*60 + \2) \3/g

...where the (=\1*60 + \2) is the magical part. I know I can insert results of evaluation with the special register =, but is there a way to do this in the subst part of a regex?

…其中(=\1*60 + \2)是魔法部分。我知道我可以使用特殊寄存器=插入计算结果,但是有办法在regex的子部分中这样做吗?

2 个解决方案

#1


24  

Something like this?

是这样的吗?

:%s/^\(\d\d\):\(\d\d\)/\=submatch(1)*60+submatch(2)/

When the replacement starts with a \= the replacment is interpreted as an expression.

当替换以\=开始时,替换被解释为表达式。

:h sub-replace-expression is copied below

:下面复制h子替换表达式

Substitute with an expression           *sub-replace-expression*
                        *sub-replace-\=*
When the substitute string starts with "\=" the remainder is interpreted as an
expression.  This does not work recursively: a substitute() function inside
the expression cannot use "\=" for the substitute string.

The special meaning for characters as mentioned at |sub-replace-special| does
not apply except for "<CR>", "\<CR>" and "\\".  Thus in the result of the
expression you need to use two backslashes to get one, put a backslash before a
<CR> you want to insert, and use a <CR> without a backslash where you want to
break the line.

For convenience a <NL> character is also used as a line break.  Prepend a
backslash to get a real <NL> character (which will be a NUL in the file).

When the result is a |List| then the items are joined with separating line
breaks.  Thus each item becomes a line, except that they can contain line
breaks themselves.

The whole matched text can be accessed with "submatch(0)".  The text matched
with the first pair of () with "submatch(1)".  Likewise for further
sub-matches in ().

#2


4  

Use submatch() to refer to a grouped part in the substitution place:

使用submatch()引用替换位置中的分组部分:

:%s/\v^(\d{2}):(\d{2})>/\=submatch(1) * 60 + submatch(2)/

With your example yields:

与你的示例收益率:

173 rest of line 1...
190 rest of line 2...
334 rest of line 3...
335 rest of line 4...
602 rest of line 5...

#1


24  

Something like this?

是这样的吗?

:%s/^\(\d\d\):\(\d\d\)/\=submatch(1)*60+submatch(2)/

When the replacement starts with a \= the replacment is interpreted as an expression.

当替换以\=开始时,替换被解释为表达式。

:h sub-replace-expression is copied below

:下面复制h子替换表达式

Substitute with an expression           *sub-replace-expression*
                        *sub-replace-\=*
When the substitute string starts with "\=" the remainder is interpreted as an
expression.  This does not work recursively: a substitute() function inside
the expression cannot use "\=" for the substitute string.

The special meaning for characters as mentioned at |sub-replace-special| does
not apply except for "<CR>", "\<CR>" and "\\".  Thus in the result of the
expression you need to use two backslashes to get one, put a backslash before a
<CR> you want to insert, and use a <CR> without a backslash where you want to
break the line.

For convenience a <NL> character is also used as a line break.  Prepend a
backslash to get a real <NL> character (which will be a NUL in the file).

When the result is a |List| then the items are joined with separating line
breaks.  Thus each item becomes a line, except that they can contain line
breaks themselves.

The whole matched text can be accessed with "submatch(0)".  The text matched
with the first pair of () with "submatch(1)".  Likewise for further
sub-matches in ().

#2


4  

Use submatch() to refer to a grouped part in the substitution place:

使用submatch()引用替换位置中的分组部分:

:%s/\v^(\d{2}):(\d{2})>/\=submatch(1) * 60 + submatch(2)/

With your example yields:

与你的示例收益率:

173 rest of line 1...
190 rest of line 2...
334 rest of line 3...
335 rest of line 4...
602 rest of line 5...