-3

I want to create validNumber regular expression, but how can I handle a number ".digit" (ex, '.2') case?

import re
m = '[-|+]?[[\.\d+]|[\d+\.?\d*]]'

re.findall(m, '.2')

Returns ['.', '2'], expected ['.2'].

Anonymous
  • 11,740
  • 3
  • 40
  • 50
catinea
  • 1
  • 1

1 Answers1

0

I think '[-|+]?(\.\d+|\d+\.?\d*)' will miss negative numbers; assuming the op wanted the - and + as part of the match, '[+|-]*\d*\.?\d+' might work better:

>>> m = '[+|-]*\d*\.?\d+'
>>> re.findall(m,'.2 fun stuff-13t 34.5s 234.')
['.2', '-13', '34.5', '234']
Leaf Nunes
  • 21
  • 3