I trying to detect all occurrences of a certain string, that is not surrounded by certain strings (using regex lookarounds). Eg. all occurrences of "African" but not "South African Society". See a simplified example below.
#My example text:
text <- c("South African Society", "South African",
"African Society", "South African Society and African Society")
#My code examples:
str_detect(text, "(?<!South )African(?! Society)")
#or
grepl("(?<!South )African(?! Society)", perl=TRUE , text)
#I need:
[1] FALSE TRUE TRUE TRUE
#instead of:
[1] FALSE FALSE FALSE FALSE
The problem seems to be that regex evaluates the lookbehind and the lookahead separately and not as a whole. It should require both conditions and not only one.