您需要[.,]
通过?
在该字符类之后添加来使零件成为可选零件,并且不要忘记添加锚点。^
断言我们处于起点,$
断言我们处于终点。
^\d*[.,]?\d*$
>>> import re
>>> r = re.compile(r"^\d*[.,]?\d*$")
>>> if r.match('0.1.'): print 'it matches!'
...
>>> if r.match('0.abc'): print 'it matches!'
...
>>> if r.match('0.'): print 'it matches!'
...
it matches!
如果您不想使用单个逗号或点,请使用前瞻。
^(?=.*?\d)\d*[.,]?\d*$