Search code examples
regexemacsoperation

Emacs : Search and replace operations


I'm looking for a way to search and replace regexp with Emacs while also doing operations. I don't really know how to explain it but here is an example :

Let's say I have this code going on for 200+ lines :

"lq.u $r40r41 = 0[$r39]\n"
"addd $r14r15 = $r14$r15, $r40$r41\n"
";;\n"              
"lq.u $r42r43 = 16[$r39]\n"             
"addd $r16r17 = $r16$r17, $r42$r43\n"
";;\n"    

And I made a mistake in the choice of my registers so I want to change them into :

"load $r39r40 = 0[$r39]\n"
"add $r13r14 = $r13$r14, $r39$r40\n"
";;\n"              
"load $r41r42 = 16[$r39]\n"             
"addd $r15r16 = $r15$r16, $r41$r42\n"
";;\n"  

So basically, what I would like would be a way to regexp search 2 digit numbers "XX" and replace them by "XX-1". Is there a way to do this through Emacs or else ?

I managed to save some time from manual editing using Excel sheets but I need a more efficient and less dummy way of doing that.


Solution

  • I can't tell whether there should always be a $ before the r, so either:

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

    or:

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

    Where:

    • \, evaluates a lisp expression in the replacement, and hence:
    • \,(1- ...) is a call to the 1- function, with the argument:
    • \#1 which, within this replacement syntax, represents group 1 of the match as a number.