Emacs:正则表达式替换以更改大小写 [英] Emacs: regular expression replacing to change case

查看:27
本文介绍了Emacs:正则表达式替换以更改大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每隔一段时间我想替换所有值的实例,例如:

Every once in a while I want to replace all instances of values like:

<BarFoo>

<barfoo>

即用正则表达式替换尖括号内的所有内容,用小写字母等效.

i.e. do a regular expression replace of all things inside angle brackets with its lowercase equivalent.

有人有一个很好的 Lisp 片段吗?可以安全地假设我们只处理 ASCII 值.任何足以采用完整正则表达式的通用内容的奖励积分,而不仅仅是处理尖括号示例.仅使用 M-x query-replace-regexp 的答案还有更多奖励点.

Anyone got a nice snippet of Lisp that does this? It's safe to assume that we're dealing with just ASCII values. Bonus points for anything that is generic enough to take a full regular expression, and doesn't just handle the angle brackets example. Even more bonus points to an answer which just uses M-x query-replace-regexp.

谢谢,

多姆

推荐答案

Try Mx query-replace-regexp with "<([^>]+)>" 作为搜索字符串,"<,(downcase 1)>" 作为替换.

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

这应该适用于 Emacs 22 及更高版本,请参阅此 Steve Yegge 博客文章 了解有关如何在替换字符串中使用 Lisp 表达式的更多详细信息.

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 the replacement string.

对于早期版本的 Emacs,您可以尝试这样的操作:

For earlier versions of Emacs you could 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))))

这篇关于Emacs:正则表达式替换以更改大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆