开发者

regular expression to match -100 to 0

开发者_如何学JAVAI need a regular expression to match all numbers inclusive between -100 and 0.

So valid values are:

  • 0
  • -100
  • -40

Invalid are:

  • 1
  • 100
  • 40

Thank you!


Use this function:

/^(?:0|-100|-[1-9]\d?)$/


OK, so I'm late, but here goes:

(?:         # Either match:
 -          # a minus sign, followed by
 (?:        # either...    
  100       # 100
  |         # or
  [1-9]\d?  # a number between 1 and 99
 )
 |          # or...
 (?<!-)     # (unless preceded by a minus sign)
 \b0        # the number 0 on its own
)
\b          # and make sure that the number ends here.
(?!\.)      # except in a decimal dot.

This will find negative integer numbers (-100 to -1) and 0 in normal text. No leading zeroes allowed.

If you already have the number isolated, then

^(?:-(?:100|[1-9]\d?)|0)$

is enough if you don't want to allow leading zeroes or -0.

If you don't care about leading zeroes or -0, then use

^-?0*(?:100|\d\d?)$

...Now what do you do if your boss tells you "Oh, by the way, from tomorrow on, we need to allow values between -184.78 and 33.53"?


How about using a capture group and then programmatically testing the value e.g.

(-?\p{Digit}{1,3})

and then testing the captured value to ensure that it is within your range?


Try ^(-[1-9][0-9]?|-100|0)$

But perhaps it would be simpled to cast it to numeric and quickly check the range then


I'm new to regular expressions would this work? (-100|((-[1-9]?[0-9])|\b0))

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜