Emacs regexp for array increase / decrease indices

Can regex be used to change array index in emacs?

eg. for some C code change:

int my_array[4]; my_array[0] = 1; my_array[1] = 2; my_array[2] = 3; 

in

 int my_array[4]; my_array[1] = 1; my_array[2] = 2; my_array[3] = 3; 

replace [i] with [i+1] operation?

+4
source share
2 answers

Something like that?

  Mx query-replace-regexp my_array\[\([0-9]+\)\] RET my_array[\,(1+ \#1)] 

\, in the replacement string, any lisp expression can be replaced.

(last edit: using \#1 instead of (string-to-int \1) )

+11
source

increment:

 Mx query-replace-regexp \[\([0-9]+\)\] RET [\,(1+ \#1)] 

decrement:

 Mx query-replace-regexp \[\([0-9]+\)\] RET [\,(1- \#1)] 
+1
source

Source: https://habr.com/ru/post/1385856/


All Articles