s = r'abc123d, hello 3.1415926, this is my book'print re.findall(r'-?[0-9]+(?:.[0-9]*)?|-?.[0-9]+',s)
使用原始模式时,你无需两次逃脱。
输出:
['123', '3.1415926']
同样,返回类型将是字符串列表。如果要将返回类型设置为整数和浮点数,请使用
map
import re,asts = r'abc123d, hello 3.1415926, this is my book'print map(ast.literal_eval,re.findall(r'-?[0-9]+(?:.[0-9]*)?|-?.[0-9]+',s))
输出:
[123, 3.1415926]



