Q:

extracting Middle name using R

I am new to R language I am facing problem to extract the middle name of a name like
Carlsson, Mr. Frans Olof
Heininen, Miss. Wendla Maria
Hays, Mrs. Charles Melville
here I want to select only the salutation for the name like Mr or Miss etc used in the middle of the name. Guide me that how can I do it.

A:

Try
 gsub(".*, +([^ ]+).*", "\\1", str1)
 #[1] "Mr."   "Miss." "Mrs." 

Or using str_extract from stringr
 library(stringr)
 str_extract(str1, perl("(?<=, )[^ ]+(?= +)"))
 #[1] "Mr."   "Miss." "Mrs." 

Or stri_extract from stringi
 stri_extract(str1, regex="(?<=, )[^ ]+(?= +)")
 #[1] "Mr."   "Miss." "Mrs." 

data
 str1 <- c('Carlsson, Mr. Frans Olof','Heininen, Miss. Wendla Maria',
           'Hays, Mrs. Charles Melville')

