How to remove excess empty space in a string using Regex

I have a number of paragraphs that I want to parse using regular expressions, but unfortunately the paragraph appears with many spaces between sentences, and sometimes words. I would like to remove all the extra spaces, but I'm not sure how ... Does anyone have any ideas? I don’t want to remove all spaces, which is the only thing I have found so far, but to keep the paragraph format regular, just like after every word has a space and there are spaces after each punctuation + word. I am coding in Perl.

Any help would be appreciated!

+6
regex perl
source share
3 answers

Canonize horizontal spaces:

s/\h+/ /g; 

Canonize vertical spaces:

 s/\v+/\n/g; 

Canonize all spaces:

 s/[\h\v]+/ /g; 
+14
source share

You can use simple perl regular expression

 s/\s+/ /g; 

FYI, this kind of thing is best done with the unix tr tool

 tr -s ' ' 

Compresses spaces to one place.

+4
source share

Both of the above answers give the result, but in both cases there is a space at the beginning and end of the line.

If I use this regular expression in the string "hello world" , I get the answer as "peace hello"

To get the result as β€œhello world” , the following perl regex will work just fine

 $a =~ s/(?<!\w) //g; 

Using appearance, all extra intervals are deleted.

+1
source share

All Articles