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:
^\d(\d)?(\d)?$
^[0-9]{1,3}$
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.
i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.
A backreference in a regular expression identifies a previously matched group and looks for exactly the same text again. A simple example of the use of backreferences is when you wish to look for adjacent, repeated words in some text. The first part of the match could use a pattern that extracts a single word.
Try this:
^([1-9][0-9]{0,2}|1000)$
[1-9][0-9]{0,2}
matches any number between 1
–999
1000
matches 1000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With