Thursday, March 15, 2012

replace-regexp

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 text being replaced. `\D’ in NEWSTRING, where D is a digit, stands for whatever matched the D’th parenthesized grouping in REGEXP. For example,

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

would replace (for example) `cadr’ with `cadr-safe’ and `cddr’ with `cddr-safe’.

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

would perform exactly the opposite replacements. To include a `\’ in the text to replace with, you must give `\\’.

M-x -replace-regexp \([0-9]\)1, \1a,

would replace smth like SLK21, into SLK2a,