I'm trying to write a function that would allow me to paste written text, and it would return a list of the in-text citations that were used in the writing. For example, this is what I currently have:
pull_cites<- function (text){
gsub("[\\(\\)]", "", regmatches(text, gregexpr("\\(.*?\\)", text))[[1]])
}
pull_cites("This is a test. I only want to select the (cites) in parenthesis. I do not want it to return words in
parenthesis that do not have years attached, such as abbreviations (abbr). For example, citing (Smith 2010) is
something I would want to be returned. I would also want multiple citations returned separately such as
(Smith 2010; Jones 2001; Brown 2020). I would also want Cooper (2015) returned as Cooper 2015, and not just 2015.")
And in this example, it returns
[1] "cites" "abbr" "Smith 2010"
[4] "Smith 2010; Jones 2001; Brown 2020" "2015"
But I would want it to return something like:
[1] "Smith 2010"
[2] "Smith 2010"
[3] "Jones 2001"
[4] "Brown 2020"
[5] "Cooper 2015"
Any ideas on how to make this function more specific? I am using R. Thanks!