-3

I have this string:

"White, Mr. George Voultsios"

And I would like to extract the part between the space and the dot:

"Mr"

Stibu
  • 13,247
  • 5
  • 50
  • 67

2 Answers2

0
x<-"White, Mr. George Voultsios"
sub(".* ","",sub("\\..*","",x))
[1] "Mr"
Erdem Akkas
  • 1,964
  • 8
  • 15
0

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:

  • look for a position following a space ((?<= ))
  • then capture any number of characters (.*)
  • until you get to a position that is followed by a dot ((?=\\.))
ikop
  • 1,589
  • 10
  • 23