无法编写正确的正则表达式来删除多个空格

发布于 2024-10-17 10:20:50 字数 186 浏览 8 评论 0原文

$ echo "Anirudh   Tomer" | sed 's/ +/ /g'
Anirudh   Tomer

我希望它能删除 Anirudh 和 Tomer 之间的 3 个空格,并给出结果为 "Anirudh Tomer"

我是初学者。 预先感谢您的帮助。

$ echo "Anirudh   Tomer" | sed 's/ +/ /g'
Anirudh   Tomer

I was expecting it to remove those 3 spaces between Anirudh and Tomer and give me result as "Anirudh Tomer"

I am a beginner.
Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-10-24 10:20:50

您需要使用 -r 标志启用 sed 的扩展正则表达式支持。

echo "Anirudh   Tomer" | sed -r 's/ +/ /g'

在扩展正则表达式中,?+| 元字符不得转义(请参阅维基百科)。 * 元字符之所以有效,是因为它属于基本正则表达式

You need to enable sed's extended regexp support with the -r flag.

echo "Anirudh   Tomer" | sed -r 's/ +/ /g'

In extended regular expressions, the ?, + and | metacharacters must not be escaped (see wikipedia). The * metacharacter works because it belongs to the basic regular expressions.

稚气少女 2024-10-24 10:20:50

与 VIM 正则表达式类似,您需要使用反斜杠转义 + 量词:

sed 's/ \+/ /g'

Similar to VIM regex, you need to escape the + quantifier with a backslash:

sed 's/ \+/ /g'

微凉徒眸意 2024-10-24 10:20:50
echo "Anirudh   Tomer" | tr -s ' '
echo "Anirudh   Tomer" | tr -s ' '
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文