39

What's the most elegant way to extract the last word in a sentence string?

The sentence does not end with a "." Words are seperated by blanks.

sentence <- "The quick brown fox"
TheFunction(sentence)

should return: "fox"

I do not want to use a package if a simple solution is possible. If a simple solution based on package exists, that is also fine.

Justin
  • 42,475
  • 9
  • 93
  • 111
user2030503
  • 3,064
  • 2
  • 36
  • 53

5 Answers5

69

Just for completeness: The library stringr contains a function for exactly this problem.

library(stringr)

sentence <- "The quick brown fox"
word(sentence,-1)
[1] "fox"
leo
  • 3,677
  • 7
  • 34
  • 46
31
tail(strsplit('this is a sentence',split=" ")[[1]],1)

Basically as suggested by @Señor O.

Roland
  • 127,288
  • 10
  • 191
  • 288
18
x <- 'The quick brown fox'
sub('^.* ([[:alnum:]]+)$', '\\1', x)

That will catch the last string of numbers and characters before then end of the string.

You can also use the regexec and regmatches functions, but I find sub cleaner:

m <- regexec('^.* ([[:alnum:]]+)$', x)
regmatches(x, m)

See ?regex and ?sub for more info.

Justin
  • 42,475
  • 9
  • 93
  • 111
  • I'd say Roland's solution to this specific problem is much simpler, but your code provides a good template for doing all sorts of targeted extractions. – Carl Witthoft Jul 15 '13 at 17:20
  • if you're looking to make the syntax even shorter, you can omit `^` (`sub` will start at the beginning on its own) and replace `[[:alnum:]]` with `\\w` – eddi Jul 15 '13 at 17:23
  • @eddi Good point. However, I like to be as explicit as possible with regular expressions and only generalize as I find cases I miss rather than trying to find cases where I've over matched. – Justin Jul 15 '13 at 17:37
  • 1
    how would the regex be like if we extract the last N words, not just the last one ? – Fagui Curtain Nov 23 '15 at 01:34
  • Thank you for providing a clean, base R answer. I want to learn regex, not just some clever wrapper for it!! – emilBeBri Mar 26 '18 at 13:02
13

Another packaged option is stri_extract_last_words() from the stringi package

library(stringi)

stri_extract_last_words("The quick brown fox")
# [1] "fox"

The function also removes any punctuation that may be at the end of the sentence.

stri_extract_last_words("The quick brown fox? ...")
# [1] "fox"
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
5

Going in the package direction, this is the simplest answer I can think of:

library(stringr)

x <- 'The quick brown fox'
str_extract(x, '\\w+$')
#[1] "fox"
eddi
  • 49,088
  • 6
  • 104
  • 155