I am trying to extract a certain number of words after a particular string.
library(stringr)
x <- data.frame(end = c("source: from animal origin as Vitamin A / all-trans-Retinol: Fish in general, liver and dairy products;", "source: Eggs, liver, certain fish species such as sardines, certain mushroom species such as shiitake", "source: Leafy green vegetables such as spinach; egg yolks; liver"))
for example to extract the 4 words following "source", I learnt from another question to use this code:
trimws(stringr::str_extract(x$end, '(?<=source:\\s)(\\w+,?\\s){4}'))
this works very well, however, if I try to select 8 words instead, I noted it does not recognize the "/" and returns NA for the first string.
trimws(stringr::str_extract(x$end, '(?<=source:\\s)(\\w+,?\\s){8}'))
The question is: is there a regex to include the special characters (or bypass them), so I can still extract the needed words? I noticed that the same happens with other characters (eg - ) or with double white space.
The expected output for 8 words should be something like that:
from animal origin as Vitamin A / all-trans-Retinol
it does not matter if it counts the / and - as words or not, as I can always adjust the number of quantifiers to be more (in my case, I do not mind extracting more than what I need).
thank you