Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter after special characters

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

like image 248
Rajan Avatar asked Dec 07 '15 14:12

Rajan


1 Answers

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 newline

The 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 uppercase

And trimws removes the initial space we added with the regex.

like image 70
Wiktor Stribiżew Avatar answered Nov 09 '22 22:11

Wiktor Stribiżew