RegEx validation of decimal numbers with comma or dot

前端 未结 4 1489
离开以前
离开以前 2020-12-16 18:22

How to validate such number input with one RegEx. Strings are not allowed. Two decimal positions after dot or comma.

Example:

123.34
1.2

4条回答
  •  执念已碎
    2020-12-16 18:49

    pat = re.compile('^\d+([\.,]\d\d)?$')
    re.match(pat, '1212')
    <_sre.SRE_Match object at 0x91014a0>
    re.match(pat, '1212,1231')
    None
    re.match(pat, '1212,12')
    <_sre.SRE_Match object at 0x91015a0>
    

提交回复
热议问题