I want to remove extra spaces, add spaces if required and capitalize first letter of each word after special character using R
string <- "apple,banana, cat, doll and donkey; fish,goat"
I want output as
Apple, Banana, Cat, Doll and donkey; Fish, Goat
I tried
gsub("(^.|,.|;.)", "\\U\\1", string, perl=T, useBytes = F)
It didn't work. Please help
You can use
string <- "apple,banana, cat, doll and donkey; fish,goat"
trimws(gsub("(^|\\p{P})\\s*(.)", "\\1 \\U\\2", string, perl=T))
## => [1] "Apple, Banana, Cat, Doll and donkey; Fish, Goat"
See this IDEONE demo
The PCRE regex matches:
(^|\\p{P})
- (Group 1) start of string or any punctuation\\s*
- 0 or more whitespace symbols(.)
- (Group 2) any character but a newlineThe replacement:
\\1
- backreferences Group 1
- inserts a space between the punctuation and the next character or at the start of string\\U\\2
- turns the Group 2 character uppercaseAnd trimws
removes the initial space we added with the regex.
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