return string with first match Regex

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

    I'd go with:

    r = re.search("\d+", ch)
    result = return r.group(0) if r else ""
    

    re.search only looks for the first match in the string anyway, so I think it makes your intent slightly more clear than using findall.

提交回复
热议问题