Suppose I have the following code:
s = \'cucumber apple tomato\'
def f(match):
if match.group(2) not in (\'apple\', ):
return \'%s (%s)\' % (match.gr
Use a capturing lookahead:
>>> s = 'cucumber apple tomato'
>>> re.findall(r'(\w+)(?=[ \t]+(\w+))', s)
[('cucumber', 'apple'), ('apple', 'tomato')]
That allows you to capture the second word in front of the first word without consuming the string.
Which you can turn into (what I >> think <<) is your desired result:
>>> [f'{t[0]} ({t[1]})' if t[1]=='apple' else t for t in re.findall(r'(\w+)(?=[ \t]+(\w+))', s)]
['cucumber (apple)', ('apple', 'tomato')]
In your comments, you have a different example with a different pattern for an answer. For that result, just use optional matches:
>>> s='cucumber apple tomato tomato apple cucumber tomato tomato'
>>> [f'{t[0]} {t[1]} ({t[2]})' if t[2] else f'{t[0]} ({t[1]})' for t in re.findall(r'(\w+)(?:[ \t]+(\w+))?(?:[ \t]+(\w+))?', s)]
['cucumber apple (tomato)', 'tomato apple (cucumber)', 'tomato (tomato)']