无法编写正确的正则表达式来删除多个空格
$ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
-r
标志启用 sed 的扩展正则表达式支持。在扩展正则表达式中,
?
、+
和|
元字符不得转义(请参阅维基百科)。*
元字符之所以有效,是因为它属于基本正则表达式 。You need to enable sed's extended regexp support with the
-r
flag.In extended regular expressions, the
?
,+
and|
metacharacters must not be escaped (see wikipedia). The*
metacharacter works because it belongs to the basic regular expressions.与 VIM 正则表达式类似,您需要使用反斜杠转义
+
量词:sed 's/ \+/ /g'
Similar to VIM regex, you need to escape the
+
quantifier with a backslash:sed 's/ \+/ /g'