Suppose I have a string like
s = "PleaseAddSpacesBetweenTheseWords"
How do I use gsub in R add a space between the words so that I get
"Please Add Spaces Between These Words"
I should do something like
gsub("[a-z][A-Z]", ???, s)
What do I put for ???. Also, I find the regular expression documentation for R confusing so a reference or writeup on regular expressions in R would be much appreciated.
You just need to capture the matches then use the \1
syntax to refer to the captured matches. For example
s = "PleaseAddSpacesBetweenTheseWords"
gsub("([a-z])([A-Z])", "\\1 \\2", s)
# [1] "Please Add Spaces Between These Words"
Of course, this just puts a space between each lower-case/upper-case letter pairings. It doesn't know what a real "word" is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With