正则表达式要求一个正数值(5,2)禁止前导零

时间:2013-09-20 08:55:44

标签: regex

正则表达式模式是否可以对数值强制执行以下约束组合?

     the number must be >= 1 and <= 999 
     (decimal point cannot be the first character in the string?)

     the number can be an integer or a number with a fractional component

     when it has a fractional component, 
     no more than 2 digits to the right of the decimal point 
     EDIT: but at least one digit to the right

     must not have leading zero(s)

3 个答案:

答案 0 :(得分:1)

Perl语法:

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

答案 1 :(得分:0)

是..

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

答案 2 :(得分:0)

我会选择这个:

/^
(?:
  999.0*                      # 999.000
  |                           # or
  [1-9]\d{,2}((?<!999)\.\d+)? # 1-998 plus optional .\d*
)
$/x