return string with first match Regex

前端 未结 6 1845
既然无缘
既然无缘 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

    You can do:

    x = re.findall('\d+', text)
    result = x[0] if len(x) > 0 else ''
    

    Note that your question isn't exactly related to regex. Rather, how do you safely find an element from an array, if it has none.

提交回复
热议问题