题目描述
请实现一个函数用来判断字符串是否表示数值 (包括整数和小数). 例如, 字符串 "+100","5e2","-123","3.1416" 和 "-1E-16" 都表示数值. 但是 "12e","1a3.14","1.2.3","+-5" 和 "12e+4.3" 都不是.
看题解考虑各种情况, 头都大了
还是正则匹配吧
- import re
- class Solution:
- # s 字符串
- def isNumeric(self, s):
- # write code here
- pattern = re.compile("[\\+\\-]?\\d*(\\.\\d+)?([eE][\\+\\-]?\\d+)?")
- return pattern.match(s).group(0)==s
- 2020-01-01 15:53:51
来源: http://www.bubuko.com/infodetail-3360564.html