-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
  • 14,192
  • 5
  • 54
  • 70

2 Answers2

0
x<-"White, Mr. George Voultsios"
sub(".* ","",sub("\\..*","",x))
[1] "Mr"
Erdem Akkas
  • 2,022
  • 9
  • 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,697
  • 1
  • 11
  • 24