Add a space between the two letters in the line in R

Suppose I have a string like

s = "PleaseAddSpacesBetweenTheseWords" 

How to use gsub in R add space between words so I get

 "Please Add Spaces Between These Words" 

I should do something like

 gsub("[az][AZ]", ???, s) 

What am I setting for ???. Also, I find the regex documentation for R confusing, so references or entries to regexes in R would be much appreciated.

+6
source share
1 answer

You just need to commit the matches, and then use the \1 syntax to refer to the captured matches. for instance

 s = "PleaseAddSpacesBetweenTheseWords" gsub("([az])([AZ])", "\\1 \\2", s) # [1] "Please Add Spaces Between These Words" 

Of course, this simply puts a space between each line of letters in lowercase / uppercase. He does not know what the real word is.

+20
source

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


All Articles