Getting captured group in one line

前端 未结 9 1031
既然无缘
既然无缘 2020-12-16 18:23

There is a known \"pattern\" to get the captured group value or an empty string if no match:

match = re.search(\'regex\', \'text\')
if match:
    value = mat         


        
9条回答
  •  春和景丽
    2020-12-16 18:41

    One line for you, although not quite Pythonic.

    find_text = lambda text: (lambda m: m and m.group(1) or '')(PATTERN.search(text))
    

    Indeed, in Scheme programming language, all local variable constructs can be derived from lambda function applications.

提交回复
热议问题