想要在emacs lisp中使用大写字母替换下划线和后跟char

时间:2014-11-18 05:28:46

标签: emacs elisp

我想使用emacs lisp将String abcd_efg替换为abcdEfg。由于某种原因,以下不起作用

 (replace-regexp-in-string "_\(.\)" "\,(capitalize \1) "abcd_efg")

但在我做的时候有效

 M-x replace-regexp _\(.\) \,(capitalize \1)

1 个答案:

答案 0 :(得分:0)

原因是\是elisp字符串中的转义字符。因此,如果要创建包含反斜杠的正则表达式,则必须编写\\

其次,调用函数时\,构造不可用。

(let ((s "abc_def_ghi_2_")
      (pos 0))
  (while (string-match "_\\(.\\)" s pos)
    (setq pos (match-end 0))
    (setq s (replace-match (capitalize (match-string 1 s)) nil nil s 1)))
  s)

返回:

"abc_Def_Ghi_2_"