Python regular expression not matching

前端 未结 3 1610
攒了一身酷
攒了一身酷 2020-11-30 15:03

This is one of those things where I\'m sure I\'m missing something simple, but... In the sample program below, I\'m trying to use Python\'s RE library to parse the string \"

3条回答
  •  [愿得一人]
    2020-11-30 15:32

    You should use re.findall instead:

    >>> line = '    0 repaired, 90.31% done'
    >>> 
    >>> pattern = re.compile("\d+[.]\d+(?=%)")
    >>> re.findall(pattern, line)
    ['90.31']
    

    re.match will match at the start of the string. So you would need to build the regex for complete string.

提交回复
热议问题