Make python re.sub look multiple times

前端 未结 2 1822
南笙
南笙 2021-01-29 07:46

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         


        
2条回答
  •  灰色年华
    2021-01-29 08:05

    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)']
    

提交回复
热议问题