Separate extra spaces when there are several lines in a row

I get a regex expression that will highlight spaces when repeating two or more repetitions, leaving only one place.

For example, this line

The cow  jumped    over the moon

which has several spaces separating words in some cases, will become

The cow jumped over the moon
+3
source share
4 answers

Try this regex:

[ ]+

and replace it with a single space.

+8
source
string cleanedString = Regex.Replace(input, @"\s+", " ");
+15
source

, , . , , , , , , . 64 7 . , 2-5 , .

0

sed.

$ echo "The cow  jumped    over the moon" | sed -e 's/[     ]\+/ /g'
The cow jumped over the moon

In the square brackets of the character class, you have a space and a tab character. I quoted "+" with "\", which might not be necessary if your regular expression engine accepts "+" to mean "one or more" and not the literal "+".

0
source

Source: https://habr.com/ru/post/1709517/


All Articles