开发者

Regex for range 1-1000 [duplicate]

开发者 https://www.devze.com 2023-01-31 21:00 出处:网络
This question already has answers here: Regular expression where part of string must be number between 0-100
This question already has answers here: Regular expression where part of string must be number between 0-100 (7 answers) Closed 1 year ago. 开发者_如何转开发

I need help creating a simple regex for a whole number range of 1-1000, with no special characters.

The two I have both seem to break or allow characters or not the full range:

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


Try this:

^([1-9][0-9]{0,2}|1000)$
  • [1-9][0-9]{0,2} matches any number between 1999
  • 1000 matches 1000


Use ^(.*[^0-9]|)(1000|[1-9]\d{0,2})([^0-9].*|)$ which will match 1000 or a non-zero digit followed by up to two further digits. It will also allow other characters on either end of the number.

0

精彩评论

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