return string with first match Regex

前端 未结 6 1858
既然无缘
既然无缘 2020-11-27 18:26

I want to get the first match of a regex.

In this case, I got a list:

text = \'aa33bbb44\'
re.findall(\'\\d+\',text)

6条回答
  •  青春惊慌失措
    2020-11-27 18:53

    Maybe this would perform a bit better in case greater amount of input data does not contain your wanted piece because except has greater cost.

    def return_first_match(text):
        result = re.findall('\d+',text)
        result = result[0] if result else ""
        return result
    

提交回复
热议问题