Match string, but only if not preceded by other string
You can use a negative lookbehind:
grep("(?<!C)lo", v, ignore.case=T, perl=T)
That will make sure that the string isn't preceded by C.
Negative Lookbehind (PCRE in R)
R uses the PCRE engine, which supports lookbehind. Do this:
grep("(?<!c)lo", subject, perl=TRUE, value=TRUE, ignore.case=TRUE);
The negative lookbehind (?<!c)
asserts that what precedes the current position is not a c
Option 2: Check for Capital Letter, Turn On Case-Insensitivity Inline
Given your input, a more general option would be to assert that lo
is not preceded by a capital letter:
grep("(?<![A-Z])(?i)lo", subject, perl=TRUE, value=TRUE);
For this option, we use the inline modifier (?i)
to turn on case-insensitivity, but only after we have checked that no capital letters precede our position.
Reference
- Inline Modifiers
- Mastering Lookahead and Lookbehind
- Lookahead and Lookbehind Zero-Length Assertions