Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs regexp to increment/decrement array indices

Is it possible to use a regular expression to alter an array index in emacs?

e.g. for some C code change:

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

to:

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

a sort of replace [i] with [i+1] operation?

like image 268
bph Avatar asked Dec 13 '22 07:12

bph


1 Answers

Something like that ?

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

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

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

like image 179
Rémi Avatar answered Dec 27 '22 21:12

Rémi