Distinguish between numbers and letters with regular expressions

For exponential notation and discrimination up to nan

import re

def isd(N):
    return bool(re.compile("^[-+]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][-+]?[0-9]+)?$|nan$|^[-+]?inf$").match(N))

for i in ['-3.', '0.', '5..', '0.1', '1', '1e+2', 'b0', '0c', 'test','nan','1ee-3','1.3e-3','1.3e-3.','00.','5.0.','inf']:
	print('{0:9} {1}'.format(i,isd(i)))

Output result

-3.       True
0.        True
5..       False
0.1       True
1         True
1e+2      True
b0        False
0c        False
test      False
nan       True
1ee-3     False
1.3e-3    True
1.3e-3.   False
00.       True
5.0.      False
inf       True

Recommended Posts

Distinguish between numbers and letters with regular expressions
Extract numbers with regular expressions
Sorting with mixed numbers and letters
About Python and regular expressions
Get rid of dirty data with Python and regular expressions
Handling regular expressions with PHP / Python
Ansible Jinja2 filters Replace and extract variable strings with regular expressions
Communicate between Elixir and Python with gRPC
Overlapping regular expressions in Python and Java
difference between statements (statements) and expressions (expressions) in Python
Replace non-ASCII with regular expressions in Python
Python: Simplified morphological analysis with regular expressions
[Python] Get rid of dating with regular expressions
Mutual conversion between fixed point and binary numbers
Script to tweet with multiples of 3 and numbers with 3 !!
Ten Puzzle-Make 10 with only 4 numbers and 4 arithmetic operations
Remove extra strings in URLs with regular expressions
[Python] Regular Expressions Regular Expressions
FizzBuzz with regular expressions etc. without using the'%' operator
Let's distinguish between data structure manipulation and logic code.
Generate Fibonacci numbers with Python closures, iterators, and generators
Converts numbers with commas and triangles to numeric types.