www.delorie.com/gnu/docs/emacs/emacs_101.html   search  
 
Buy the book!


GNU Emacs Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

K.7.2 Regexp Replacement

The M-x replace-string command replaces exact matches for a single string. The similar command M-x replace-regexp replaces any match for a specified pattern.

In replace-regexp, the newstring need not be constant: it can refer to all or part of what is matched by the regexp. `\&' in newstring stands for the entire match being replaced. `\d' in newstring, where d is a digit, stands for whatever matched the dth parenthesized grouping in regexp. To include a `\' in the text to replace with, you must enter `\\'. For example,

 
M-x replace-regexp RET c[ad]+r RET \&-safe RET

replaces (for example) `cadr' with `cadr-safe' and `cddr' with `cddr-safe'.

 
M-x replace-regexp RET \(c[ad]+r\)-safe RET \1 RET

performs the inverse transformation.


  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003