import re class Solution(object): def isNumber(self, s): pattern = re.compile('^[ \t]*[+-]?(((\d)+(\.)?(\d)*)|((\d)*(\.)?(\d)+))(e[+-]?\d+)?[ \t]*$') return True if pattern.match(s) else False
^ -> string beginning
[ \t]* -> matches any spaces and tabs in the beginning
[+-]? -> matches any + or - sign in the beginning
(((\d)+(\.)?(\d)*)|((\d)*(\.)?(\d)+)) -> matches numbers in the formats n.n, .n and n., where n is a number with only digits, and doesn't match just a '.'. That was the reason for the or, because an asterisk on both sides of the dot would allow for just a dot to be accepted.
((\d)+(\.)?(\d)*) matches at least one number before the dot,
((\d)*(\.)?(\d)+) matches at least one number after the dot.
(e[+-]?\d+)? -> matches the e part, including + and - signs
[ \t]* -> matches spaces and tabs in the end
$ -> string end