在Emacs中将一个空格替换成两个句子 [英] Replace one space by two after sentences in Emacs

查看:213
本文介绍了在Emacs中将一个空格替换成两个句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档,其中一些句子以点和一个空格(。Nextline)结尾,而其他句子以点和两个空格(。Nextline)。我想将替换点和一个空格替换为点和两个空格,但不会增加现有的点和两个空格到点和三个空格。



除了最后一个,段落的句子不会以换行符或\\\
结尾。在段落末尾会有一个换行符。我想用2个空格开始每个句子,不是1个或3个以上。如果我从菜单中使用搜索和替换,从2个空格开始的句子开始时会增加到3个空格。



我该怎么做?我尝试以下,但它增加两个空格到三:

 (defun space12()
(interactive)
(while(re-search-forward\\。?nil t)
(replace-match。)))

问题出在哪里,怎么纠正?



样本输入文本:

 这是第一句它早一点)。这句话在开始时有一个空格。这有两个。而在开始时这又有一个空间。 


解决方案

您可以使用 Mx query-replace-regexp )与搜索字符串 \。 \([^] \)和替换字符串。 \1



当您要将 \ 放在字符串中时你必须用另一个 \ 来逃避它。例如:

 (defun space12()
(interactive)
(while(re-search-转发\\。\\([^] \\)nil t)
(replace-match。\\1t)))

为了获得这个表现良好,我会让它总是处理整个缓冲区,并返回到原来的位置: / p>

 (defun space12()
(interactive)
(save-excursion
(goto- char(point-min))
(while(re-search-forward\\。\\([^] \\)nil t)
(replace-match 。\\1t))))


I have a document with paragraphs where some sentences end with a dot and one space (". Nextline"), while others end with a dot and two spaces (". Nextline"). I want to replace replace dot and one space to dot and two spaces but without increasing existing dot and two spaces to dot and three spaces.

The sentences of paragraph are not ending with newline or "\n", except the last one. There will be a newline character at the end of the paragraph. I want to start each sentence with 2 spaces, neither 1 nor 3 or more. If I use search and replace from the menu, sentences starting with 2 spaces increase to 3 spaces at their start.

How can I do that? I tried following but it increases two spaces to three:

(defun space12 ()
    (interactive)
    (while (re-search-forward "\\. ?" nil t)
        (replace-match ".  ")))

Where is the problem and how can I correct it.

Sample input text:

This is first sentence (I called it line earlier). This sentence has one space at start.  This has two. And this again has one space at start.

解决方案

You can use C-M-% (M-x query-replace-regexp) with search string \. \([^ ]\) and replacement string . \1.

When you want to put \ in a string remember that you have to escape it with another \. For example:

(defun space12 ()
  (interactive)
  (while (re-search-forward "\\. \\([^ ]\\)" nil t)
    (replace-match ".  \\1" t)))

To get this "well behaved" I'd make it always process the whole buffer and go back to the original position after:

(defun space12 ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "\\. \\([^ ]\\)" nil t)
      (replace-match ".  \\1" t))))

这篇关于在Emacs中将一个空格替换成两个句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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