开发者

RegEx number [-99.99..-0.01]+[0.01..99.99]

I want to validate a number. This number must be from -99.99 to +99.99 but except 0 (zero). I came up with this RegEx expression

[-+]?((\d{1,2}\.?\d{0,1}[1-9])|(\d{0,1}[1-9]))

but it does not handle number of this type:

x0
xy.00

Any idea?

Edit: I am trying to do 开发者_开发百科a restriction for xsd file.


Try:

[+-]?([1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9])

meaning:

[+-]?                 # optional '+' or '-'
(                     #
  [1-9]\d?(\.\d\d?)?  #   1 - 99.99
  |                   #   OR
  0\.[1-9]\d?         #   0.1 - 0.99
  |                   #   OR
  0\.0[1-9]           #   0.01 - 0.09
)                     #


Try this regular expression:

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

This will allow any number starting with a digit greater than 0 followed by an additional optional digit ([1-9]\d?(\.\d{1,2})?). Or, if it starts with 0, followed by a decimal point and followed by either a sequence that does not allow 00 nor 0 (0\.(\d?[1-9]|[1-9]\d)).


Well, you can simply add two more cases for the missing numbers ... starting from your original regex,

 [-+]?((\d{1,2}\.?\d{0,1}[1-9])|(\d{0,1}[1-9])|(\d0)|([1-9]\d\.00)|(\d[1-9].00))

BTW rather than \d{1,2} it seems slightly better to write \d\d?; similarly \d? rather than \d{0,1}.

In any case this seems an exercise in pain. Can't you use the regex to verify that this is a number in the format you want it, and use a separate constraint to make it not be zero?


try this

^\d{0,2}(\.\d{1,2})?$/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜