How can I use vim regex to replace text when math divide is involved in the expression

左心房为你撑大大i 提交于 2019-12-10 15:19:24

问题


I am using vim to process text like the following

0x8000   INDEX1 ....
0x8080   INDEX2 ....
....
0x8800   INDEXn ....

I want to use regular expression to get the index number of each line. that is

0x8000 ~ 0
0x8080 ~ 1
....
0x8800 ~ n

The math evaluation should be (hex - 0x8000) / 0x80. I am trying to using vim regular expression substitution to get the result in line

%s/^\(\x\+\)/\=printf("%d", submatch(1) - 0x8000)

This will yield

0     INDEX0
128   INDEX1
....
2048  INDEXn

What I want to do is to further change it to

0     INDEX0
1     INDEX1
...
20    INDEXn

That is, I want to further divide the first column with an 0x80. Here is when I get the problem.

The original argument is "submatch(1) - 0x8000". I now add an "/ 0x80" to it, which forms

%s/^\(\x\+\)/\=printf("%d", (submatch(1) - 0x8000)\/0x80)

Now Vim report error

Invalid expression: printf("%d", (submatch(1) - 0x8000)\/0x80))

It looks like vim meet problem when processing "/". I also tried with a single "/" (without escape), but still fails.

Can anyone help me on this?


回答1:


You can't use the separation character in a sub-replace-expression.
From :h sub-replace-expression :

Be careful: The separation character must not appear in the expression!
Consider using a character like "@" or ":".  There is no problem if the result
of the expression contains the separation character.

Instead, change the separator to no longer match the division operator. E.g., use #.

:%s#^\(0x\x\+\)#\=printf("%d", (submatch(1) - 0x8000)/0x80)

Note that I had to change your regex (specifically ^\(\x\+\) to ^\(0x\x\+\)). I don't know why yours works for you, but from :h character-classes, \x shouldn't include the trailing 0x :

/\x     \x      \x      hex digit:                      [0-9A-Fa-f] 

Also, your regex is a bit easier to read (to me at least) using very-magic mode (see :h magic):

:%s#\v^(0x\x+)#\=printf("%d", (submatch(1) - 0x8000)/0x80)


来源:https://stackoverflow.com/questions/30072389/how-can-i-use-vim-regex-to-replace-text-when-math-divide-is-involved-in-the-expr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!