I\'m trying to dig deeper into regexes and want to match a condition unless some substring is also found in the same string. I know I can use two grepl
statem
You can use the anchored look-ahead solution (requiring Perl-style regexp):
grepl("^(?!.*park)(?=.*dog.*man|.*man.*dog)", x, ignore.case=TRUE, perl=T)
Here is an IDEONE demo
^
- anchors the pattern at the start of the string(?!.*park)
- fail the match if park
is present(?=.*dog.*man|.*man.*dog)
- fail the match if man
and dog
are absent.Another version (more scalable) with 3 look-aheads:
^(?!.*park)(?=.*dog)(?=.*man)