I have a list of lowercase words on separate lines. I want to match all lines which contain the letter 'r' where it is NOT preceded by 'e' (er), NOR by 'fo' (for).
Here is a sample list:
fortnight
reject
forrest
fourteen
certain
forester
heretofore
example
Here are the words that I want to match:
reject
forrest
fourteen
Here are two regexes, each of which does half of what I want:
^(.*((?<!e)r).*)$
^(.*(r(?<!fo)r).*)$
The first matches all words except 'certain'; the second all words except 'fortnight', so the words I want are matched. However the words that contain both 'for' and 'er', with no other 'r' are also matched. Applying one regex after the other fails to eliminate the false positives.
How can I combine the two regexes with an AND condition, so that both are applied in one expression?
You can use a simple alternation as
^\w*(?<!e|fo)r\w*$
for example http:http://regex101.com/r/mW5qZ9/6
What it does?
(?<!e|fo)
asserts that the regex is not presceded by e
or fo
r
matches character r
The regex would match
fourteen
reject