import re
# 判断一个字符串是否是小数
def IsFloat(s):
pattern = '^-?\d+\.?\d*$' # 匹配数字: 从头开始匹配 -0或1次 数字1或多次 .0或1次 数字0或多次 匹配到字符串末尾
match = re.match(pattern, s)
return match != None
还是有缺陷, 123. 可能会被识别为正确,不过在python里这代表 123.0
https://www.runoob.com/python/python-reg-expressions.html#flags