Emacs regex groups in regex-replace

I have a bunch of C macros in files, for example, NEXT( pL ) , which expands to ( ( pL ) -> next )

I want to remove most of them because they are not needed.

What I would like to do is get the text inside the brackets in the macro, pL . I want the regex replacement to use this text for rewriting. For example, in Perl, I could do something like /NEXT\(\s*(.+)\s*) (maybe a little wrong), and then output something like $1->next , which should include the line

 if ( NEXT( pL ) != NULL ) { 

in

 if ( pL->next != NULL ) { 

In Emacs, I would like to use match groups in emacs replace-regexp in file by file. I'm not quite sure how to do this in Emacs.

+4
source share
1 answer
 Mx query-replace-regexp NEXT(\([^)]+\)) RET \1->next RET 

What can be done inside such a function (in relation to the entire buffer)

 (defun expand-next () "interactive" (goto-char (point-min)) (while (re-search-forward "\\<NEXT(\\([^\)]+\\))" nil t) (replace-match "\\1->next"))) 

And to apply this to multiple files, you can mark the files in Dired and type Q to do query-replace-regexp for all the marked files (use regex / replace in the first line of this answer).

+12
source

All Articles