首页 > 解决方案 > 使用正则表达式查找和替换 - 仅删除字母之间的双空格

问题描述

尝试在 Atom 编辑器(1.39.1 x64,uBuntu 18.04)中执行此操作,但假设这适用于使用正则表达式的其他文本编辑器。

假设我们有这个文本:

This text has some double-spaces. Lets try to remove them. But not after a full-stop or if three or more spaces.

我们想更改为:

This text has some double-spaces. Lets try to remove them. But not after a full-stop or if three or more spaces.

使用Regex启用的 ( .*) 查找,使用以下方法正确找到所有匹配项:[a-zA-Z] [a-zA-Z]。但是Replace行中执行逻辑的内容是:

1st letter, single space, 2nd letter?

标签: regexreplaceatom-editor

解决方案


你可以用这个

([a-z])\s{2}([a-z])

并替换为$1 $2

在此处输入图像描述

Regex Demo

如果您的编辑器支持环视,您可以使用

(?<=[a-z])\s{2}(?=[a-z])

替换为单个空格字符

Regex demo

注意:- 不要忘记使用i标志来区分大小写或将字符类更改为[a-zA-Z]


推荐阅读