-2

I want to extract a name field from a text eg

name = "My name is John Smith"

should return John Smith

My current code is

grep(".^[A-Z][a-z]+\\s[A-Z][a-z]+", name, value = TRUE)
JustCarty
  • 3,839
  • 5
  • 31
  • 51
yash deo
  • 21
  • 2

1 Answers1

4

We can use sub to capture the words that start with uppercase, followed by lower case, then a space followed by the word with upper case, lower case letters of the string followed by other characters (.*) and replace with the backreference (\\1) of the captured group

sub(".*([A-Z][a-z]+\\s[A-Z][a-z]+).*", "\\1", name)
#[1] "John Smith"

edit: added @DJack's recommendation

data

name <- c("My name is John Smith")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    To cover more cases, in particular when the name is in the middle of the text, you may want to replace `$` by `.*` – DJack Feb 19 '18 at 08:50