Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

vim

eval

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?

like image 895
Eric Sun Avatar asked May 06 '15 09:05

Eric Sun


1 Answers

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)
like image 109
Marth Avatar answered Sep 22 '22 06:09

Marth