我需要一个正则表达式,能够在两个或更多连续的空格中去除空格,只留下一个空格。
例如,这一行:
在某些情况下,具有多个空格分隔单词的文本将变为:
例如,这一行:
The cow jumped over the moon
在某些情况下,具有多个空格分隔单词的文本将变为:
The cow jumped over the moon
string cleanedString = Regex.Replace(input, @"\s+", " ");
尝试使用这个正则表达式:
[ ]+
将其替换为单个空格。
我经常使用sed来完成这个任务。
$ echo "The cow jumped over the moon" | sed -e 's/[ ]\+/ /g'
The cow jumped over the moon