Emacs: replacing a regex with a change

From time to time, I want to replace all instances of values ​​such as:

<BarFoo> 

from

 <BarFoo> 

i.e. make a regex to replace all things inside angle brackets with its lower equivalent.

Has anyone got a nice Lisp snippet that does this? It is safe to assume that we are dealing only with ASCII values. Bonus points for everything that is common enough to accept a full regular expression and don't just handle the example of angle brackets. More bonuses point to an answer that simply uses Mx query-replace-regexp .

Thank,

House

+43
regex replace emacs
Mar 24 '09 at 11:33
source share
1 answer

Try Mx query-replace-regexp with "<\([^>]+\)>" as the search string and "<\,(downcase \1)>" as the replacement.

This should work for Emacs 22 and later, see this Steve Yegge blog post for more details on how Lisp expressions can be used in a replacement string.

For earlier versions of Emacs, you can try something like this:

 (defun tags-to-lower-case () (interactive) (save-excursion (goto-char (point-min)) (while (re-search-forward "<[^>]+>" nil t) (replace-match (downcase (match-string 0)) t)))) 
+64
Mar 24 '09 at 11:38
source share



All Articles