We are looking for a regular expression that will remove single characters from the string, with several conditions. One regular expression deletes all single characters in a string, and another regular expression deletes only single characters between the first and last characters. See examples below.
Remove all single characters:
Before
names <- c("John C. Smith", "Chris T. Anderson", "Mary H. Jane",
"J. J. Smith", "J. Thomas")
After:
"John Smith", "Chris Anderson", "Mary Jane", "Smith", "Thomas"
Deletes single characters, excluding the first and last characters
Before
names <- c("John C. Smith", "Chris T. Anderson", "Mary H. Jane",
"J. J. Smith", "J. Thomas")
After:
"John Smith", "Chris Anderson", "Mary Jane", "J. J. Smith", "J. Thomas"
source
share