library(stringr) test <- "asdf asiodjfojewl kjwnkjwnefkjnkf [asdf] fasdfads fewrw [keyword<1] keyword [keyword>1]" ## If the word is right after "[": str_extract_all(test, "(?<=\\[)keyword[^\\]\\[]*(?=])") ## If the word is anywhere betwee "[" and "]": str_extract_all(test, "(?<=\\[)[^\\]\\[]*?keyword[^\\]\\[]*(?=])")