s = r'abc123d, hello 3.1415926, this is my book'
print re.findall(r'-?[0-9]+(?:\.[0-9]*)?|-?\.[0-9]+',s)
你不需要
escape
使用时两次
raw mode
.
输出:
['123', '3.1415926']
返回类型还将是
strings
。如果希望返回类型为
integers
和
floats
使用
map
import re,ast
s = 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]