R Regex / gsub: how to collapse spaces in a string

I have a vector of sentences that were scanned from handwritten documents. There were some problems with the interval in this process:

The d og is br own. 

I was curious if there was a common template method with '_x_' or symbol-space-space and collapse the second space as follows:

 The d og is br own. --> The dog is br own. 

I am only concerned about one character between spaces ( '_x_' NOT '_xx_' ).

Any suggestions?

+6
source share
1 answer

Can

 > x<-"The d og is br own." > gsub(" (.) "," \\1",x) [1] "The dog is br own." 

or

 gsub(" ([[:alnum:]]) "," \\1",x) 

(.) matches anything ([[:alnum:]]) matches only alphanumeric characters.

+4
source

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


All Articles