Emacs正则表达式仅在空格介于<之间时才删除空格。 >人物

时间:2014-07-24 12:19:27

标签: regex emacs removing-whitespace

我正在编辑emacs中的文件,并希望使用replace-regexp命令删除其间的所有空格< >字符。例如,假设我有以下内容:

<please help me> hello everyone <HI!>

在应用replace-regexp后,我们可以获得:

<please_help_me> hello everyone <HI!>

要匹配我们可以执行的整个字符串:<[a-z]^\s[a-z]*>但是,我怎么说我只想替换空格字符?

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

正则表达式:

( )(?=[^<]+>)

替换字符串:

_

DEMO

答案 1 :(得分:1)

以下是使用elisp的一种方法 - 此示例考虑梳理整个缓冲区。我将正则表达式分为三部分,以防原始海报希望将来使用<>之间的内容做些不同的事情。如果原始海报尚未识别,Emacs有一个很好的功能M-x re-builder来测试elisp模式加工。

  
(save-excursion
  (goto-char (point-max))
  (while (re-search-backward "\\(\<\\)\\([^>]*\\)\\(\>\\)" nil t)
    (when (looking-at "\\(\<\\)\\([^>]*\\)\\(\>\\)")
      (let* (
          (start (match-beginning 0))
          (end (match-end 0)))
        (replace-regexp "\s" "_" nil start end)))))
相关问题