用双倍替换单个空格 [英] Replace single whitespaces with double

查看:91
本文介绍了用双倍替换单个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆包含文本(日记的笔录)的XML文件.句子结尾处的要求是,句点之后必须有两个空格.目前,此操作已部分完成,但并非在所有情况下都完成:有时在下一个句子的第一个字符之前的句点之后只有一个空格.

I have a bunch of XML files containing texts (transcriptions of a diary). At the end of sentences, the requirement is that there be two whitespaces after the period. At the moment, this is partially done, but not in all cases: sometimes there is only a single whitespace after the period before the first character of the next sentence.

我正在使用Windows的Gitbash,并认为sed是要使用的命令,但是我不知道正确的正则表达式.我想我需要找到:

I'm using Gitbash for Windows, and think that sed is the command to use, but I don't know the correct regular expression. I think I need to find:

句号空白[其他字符]

period whitespace [some other character]

并替换为

句点空白[下一个相同的字符]

period whitespace whitespace [the same next character]

例如,现在我们有这个:

For example, right now we have this:

<p>The spacing after this sentence (two whitespaces) is what is required. By contrast, this sentence has only a single space after the period. This is the next sentence, the last in a paragraph, which correctly has no whitespace at all after the period.</p>

我需要的是,每个句号后面都有两个空格,除了段落中的最后一个空格.

What I need is this, where each period is followed by two whitespaces, apart from the last in the paragraph.

<p>The double whitespace after this sentence is what is required. This sentence now also has a double space after the period. This is the next sentence, the last in a paragraph, which correctly has no whitespace at all after the period.</p>

推荐答案

使用sed,您可以执行以下操作:

With sed you can do this:

sed -e "s/\. \</\.  /"

这是更改

$ sed -e "s/\. \b/\.  /g" test.txt > fixed.txt
$ diff test.txt fixed.txt
1c1
< <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vehicula placerat nisl, bibendum blandit tortor pharetra ut. Morbi nec tellus ultrices, porta felis et, dapibus diam. Phasellus vehicula ante ac urna elementum lacinia.</p>
---
> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.  Maecenas vehicula placerat nisl, bibendum blandit tortor pharetra ut.  Morbi nec tellus ultrices, porta felis et, dapibus diam.  Phasellus vehicula ante ac urna elementum lacinia.</p>

这篇关于用双倍替换单个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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