I am struggling to write the correct regex
pattern to match the following condition
(contains the word
other
) OR (contains bothus
ANDcar
)
This code works as expected:
str_detect(c('us cars',
'u.s. cars',
'us and bikes',
'other'),
regex('other|((?=.*us)(?=.*car))',
ignore_case = TRUE))
[1] TRUE FALSE FALSE TRUE
However, if I try to include variations of us
(united states) such as u.s.
and u.s
then the pattern does not work anymore.
str_detect(c('us cars',
'u.s. cars',
'us and bikes',
'other'),
regex('other|((?=.*us)(?=.*u.s.)(?=.*u.s)(?=.*car))',
ignore_case = TRUE))
[1] FALSE FALSE FALSE TRUE
What is the issue here? Why is the AND
pattern working in the first case but not in the second?
Thanks!