LoginSignup
10
9

More than 5 years have passed since last update.

正規表現で数字と文字の判別

Last updated at Posted at 2015-10-15

指数表示や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)))

出力結果

-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
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9