Lucas Rezende
Lucas Rezende

Reputation: 584

How can I replace wrong spaces in a text using REGEX?

I am trying to figure out how to replace spaces in a text like the example below but I don't know how to deal with different number of spaces in the same text

This text:

E m  se guida,  a  e mpre sa  deu  ba ixa  e m 
cerca  de  $82  b ilhões   ( ma is  de  75 %)  de  se us  a t ivos.

Should be:

Em seguida, a empresa deu baixa em 
cerca de $82 bilhões (mais de 75%) de seus ativos.

Note that there are single spaces between characters and double spaces between words.

Could someone give me some light on this?

Upvotes: 0

Views: 314

Answers (1)

user3483203
user3483203

Reputation: 51165

I would approach this in two steps. First, I would use a regex to replace all of the single spaces, and then another to shorten the double spaces. To find only single spaces, you can use this regex:

(\S)\s(\S)

Next, to find double spaces, you can use this regex:

\s\s+

So first, replace single spaces with groups one and two from the first regex, and then replace double spaces with a single space using the second regex.

Using the atom editor, you can use these two regex to find and replace like this:

enter image description here enter image description here

In the second image, you do have to enter one space, it is slightly unclear from the screen shot. Hope this helps!

Upvotes: 4

Related Questions