RegEx - Exclude zero

前端 未结 6 530
抹茶落季
抹茶落季 2021-01-26 00:21

How can I exclude the 0 input in this regex? The user should be able to enter any number... but not the zero

^([0-9]*|\\d*\\.\\d{1}?\\d*)$

Than

相关标签:
6条回答
  • 2021-01-26 00:55

    This work only with natural numbers to 5 digit and limit five 0 digit at the left (you can personalize that limit)

    /^[0]{0,5}[1-9]\d{0,4}$/
    

    match:

    1
    01 .. 000001
    0100000 .. 0000010000
    

    not match:

    0
    00 .. 00000
    
    0 讨论(0)
  • 2021-01-26 00:57

    Try this: ^([1-9]+).\d+$ This requires you to enter at least 1 digit from 1-9

    Or ^([1-9]).\d$ This does not require you to enter anything but does restrict what you enter as 1-9

    0 讨论(0)
  • 2021-01-26 01:00

    The following regex

    ^(?!0*(\.0+)?$)(\d+|\d*\.\d+)$
    

    Does not match

    0
    0.0
     .0
    

    But allows

      .1
     0.01
     1.00
     1.1
    01.01
    10.10
     1
    
    0 讨论(0)
  • 2021-01-26 01:07

    If I understand your requirement, use [1-9] in place of [0-9] or \d:

    ^([1-9]*|[1-9]*\.[1-9]{1}?[1-9]*)$
    
    0 讨论(0)
  • 2021-01-26 01:12

    You just need to change the: * [0-9] for [1-9] * /d for [1-9]

    Original: ^([0-9]*|\d*\.\d{1}?\d*)$

    Solution: ^([1-9]*|[1-9]*\.[1-9]{1}?[1-9]*)$

    \d is [0-9]

    0 讨论(0)
  • 2021-01-26 01:17

    What about this:

    ^((?:[1-9][0-9]*)(?:\.[0-9]+)?)$
    

    Match:

    1
    5
    10
    22
    5000
    1.0
    10.10
    123.456
    

    No match:

    0
    00
    007
    0.0
    0.000
    0.50
    0.01
    5,432.10
    1,234,567
    10.
    

    You could further lock it down to specific ranges, as explained here:

    http://www.regular-expressions.info/numericranges.html

    0 讨论(0)
提交回复
热议问题