Is that possible to use a pattern
like this (see below) in grepl()
?
(poverty OR poor) AND (eradicat OR end OR reduc OR alleviat) AND extreme
The goal is to determine if a sentence meets the pattern using
ifelse(grepl(pattern, x, ignore.case = TRUE),"Yes","No")
For example, if x
= "end extreme poverty in the country", it will return "Yes", while if x
= "end poverty in the country", it will return "No".
An earlier post here works only for single work like poor AND eradicat AND extreme, but not work for my case. Any way to achieve my goal?
Tried this, pattern = "(?=.*poverty|poor)(?=.*eradicat|end|reduce|alleviate)(?=.*extreme)"
, but it does not work. The error is 'Invalid regexp'
For using all 3 assertions, you can group the words using a non capture group.
^(?=.*(?:poverty|poor))(?=.*extreme)(?=.*(?:eradicat|end|reduc|alleviat)).+
^
Start of string(?=.*(?:poverty|poor))
Assert either poverty OR poor(?=.*extreme)
Assert extreme(?=.*(?:eradicat|end|reduc|alleviat))
Assert either eradicat OR end OR reduc or alleviat.+
Match the whole line for exampleFor grepl, you have to use perl=T
enabling PCRE for the lookarounds.
grepl('^(?=.*(?:poverty|poor))(?=.*extreme)(?=.*(?:eradicat|end|reduc|alleviat)).+', v, perl=T)