0
def parse_distance(string):
# write the pattern
pp = re.compile("\d+")
result = pp.search(string)
if True:
# turn the result to an integer and return it
dist = int(result)
return dist
else:
return None
parse_distance("LaMarcus Aldridge misses 13-foot two point shot")
我需要从上面显示的字符串中获得13,它给了我错误,int(结果)有错误,不是字符串。所以我需要从字符串中获取数字并将其转换为整数,我该如何去做,谢谢。我怎样才能从给定的字符串提取数字
即使没有模式中的捕获组,您也可以使用group()或group(0)来获得全文匹配。 – Blckknght
@Blckknght好点,更新。谢谢。 – alecxe
非常感谢。 – user5372470