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

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

问题描述

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

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

<BarFoo>

<barfoo>

ie。做一个正则表达式替换小括号内的所有东西与其小写的等价物。

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

任何人都有一个很好的Lisp代码片段,这样做?可以假设我们只处理ASCII值。奖金积分是足够普遍的,可以采用完整的正则表达式,而不仅仅是处理尖括号的例子。更多的奖励积分,只需使用 Mx 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.

谢谢,

Dom

推荐答案

尝试 Mx query-replace-正则表达式< \([^>] + \)>作为搜索字符串和< \,(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天全站免登陆