53.表示数值的字符串(python)

题目描述

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

2020-01-01 15:53:51

猜你喜欢

转载自www.cnblogs.com/NPC-assange/p/12129055.html