I have this string:
"White, Mr. George Voultsios"
And I would like to extract the part between the space and the dot:
"Mr"
I have this string:
"White, Mr. George Voultsios"
And I would like to extract the part between the space and the dot:
"Mr"
You could use regular expressions with a lookbehind for the space and a lookahead for the dot:
## The data:
x <- c("White, Mr. George Voultsios", "LastName, Mrs. Firstname")
Using the base package:
regmatches(x, regexpr("(?<= ).*(?=\\.)", x, perl = TRUE))
# [1] "Mr" "Mrs"
Using the package stringr
:
library(stringr)
stringr::str_extract(x, "(?<= ).*(?=\\.)")
# [1] "Mr" "Mrs"
What the pattern (?<= ).*(?=\\.)
does is:
(?<= )
).*
)(?=\\.)
)