0

Imagine you have text variable like:

x = as.character("If you do not 'answer' this question, 'two' persons will 'die' in the next second")

What is the best function to return only the quotation marked words such that

> FUNCTION(x)
> [1] "answer" "two"    "die" 
Rotail
  • 1,025
  • 4
  • 19
  • 40
  • Possible duplicate of [Extracting a string between other two strings in R](https://stackoverflow.com/questions/39086400/extracting-a-string-between-other-two-strings-in-r) – ekstroem Jul 26 '17 at 22:58

2 Answers2

1

You can use the stringr package...

library(stringr)
y <- str_match_all(x,"\\'([^\\s]+)\\'")[[1]][,2]

y
[1] "answer" "two"    "die"   

If you are applying it to a vector x, str_match_all will produce a list with a 2-column matrix for each element of x. You need the second column of each.

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32
0

Here's another way using base package:

x = "If you do not 'answer' this question, 'two' persons will 'die' in the next second"

gsub( "'",
      "",
      grep(
        pattern = "'[a-z]+'",
        x = strsplit(x, " ")[[1]],
        value   = T 
        )
     )

# [1] "answer" "two"    "die" 
Ulises Rosas-Puchuri
  • 1,900
  • 10
  • 12