Getting captured group in one line

前端 未结 9 1082
既然无缘
既然无缘 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:31

    One liners, one liners... Why can't you write it on 2 lines?

    getattr(re.search('regex', 'text'), 'group', lambda x: '')(1)
    

    Your second solution if fine. Make a function from it if you wish. My solution is for demonstrational purposes and it's in no way pythonic.

提交回复
热议问题