refers to a problem in offer, please implement a function to determine whether a string represents a numeric value (including integers and decimals). For example, the strings "+ 100"," 5e2 ","-123", "3.1416" and "- 1E-16" all represent numeric values. But "12e", "1a3.14", "1.2.3", "+-5" and "12e+4.3" are not.
import re
s = "12e"
pattern = "[\ + -]? [0-9] (\. [0-9] )? (eE? [0-9] +)?"
if re.match (pattern,s)! = None:
print (" legal")
I want to write with rules, but I"m not familiar with rules. I refer to an example on the Internet, but I didn"t pass all the test cases.
another, (eE? [0-9] +)? This expression means that eE? [0-9] + exists once or not, assuming it exists, e or E must be followed by a number to meet the expression, the expression seems to be correct, do not know where there is a problem, please point out, thank you.