Stanley
Stanley

Reputation: 2816

How do you extract keywords based on surrounding characters

Trying to extract keywords from a string in R but so far unable to find a solution

Giving the string

"erj\"abc\"ejwojeowje \"123\"fjoejfoejf" 

How to extract the keywords "abc" and "123" into a character vector? I tried using strsplit based on \" but it would also return other unnecessary keywords.

Upvotes: 1

Views: 75

Answers (1)

akrun
akrun

Reputation: 887241

We can try sub

scan(text=sub('[^"]+"([^"]+)"[^"]+"(\\d+).*',
                           "\\1 \\2", str1), what='')
#[1] "abc" "123"

data

str1 <- "erj\"abc\"ejwojeowje \"123\"fjoejfoejf" 

Upvotes: 1

Related Questions