Regex for range 1-1000 [duplicate] - regex

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:
^\d(\d)?(\d)?$
^[0-9]{1,3}$

Try this:
^([1-9][0-9]{0,2}|1000)$
[1-9][0-9]{0,2} matches any number between 1–999
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.

Related

Regex to allow numeric integer values, a single zero but disable leading zeros [duplicate]

This question already has an answer here:
JS - Regex for number without leading zero, or just zero
(1 answer)
Closed 3 years ago.
i need a Regex to allow:
Numeric integer values, they can contain zeros or end in zero. (ie:
36, 200, 105) in my case delimited by 6 digits max.
Allow a single zero
Dont allow leading zeros (ie: 048 shouldn't be valid)
Currently i have /\d{1,6}/ but im missing the last part, any suggestion ?
You just need to use regex options | (like or). One option will be used to get simple 0 and the other to get all numbers starting by non 0.
^([1-9][\d]*|0)$
If you only want a maximum of 6 digits, you can use this:
^([1-9][\d]{0,5}|0)$

What's the regular expression for pipe-delimited number? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check that a String is pipe-generated numbers. There should be numbers between pipes.
Valid Strings examples: 300, 300|600.
Invalid Strings examples: 300||||600
I tried ^([\d|\d])*$. However, this still said that 300||||600 is a valid String.
Here, what you want is one number, ie \d+, followed by an undetermined number of occurrences of a pipe then a number, which would be (\|\d+)* (the pipe is escaped).
As you want it to cover the whole input this would be
^\d+(\|\d+)*$

Regex value of 0.0-5.0 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 5 years ago.
I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?
^[0-5]+(\.[0-9]{1})$
Try this regex:
^([0-4]\.[0-9]|5\.0)$
It matches any number from 0 to 4 then dot then any number.
it also matches 5.0
Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.

Validate time and allow negative values [duplicate]

This question already has answers here:
How do I include negative decimal numbers in this regular expression?
(17 answers)
Closed 6 years ago.
I use this regex to validate time entries, e.g. 08:34 - this is a valid time,
negative time e.g. -08:54 is not valid with this regEx.
Actually I don't know how to extend this regex in order to make also negative times valid:
var regex_time =/^([0-9]{2})\:([0-9]{2})$/;
You can use the regex:
^-?([01]\d|2[0-3]):([0-5]\d)$
To get a positive/negative valid 24h format. If you don't need to limit the user to 24h format, use:
^-?(\d+):([0-5]\d)$
The question mark is a quantifier that means 0 to 1 of previous item.

Regular expression for 10 numeric digits [duplicate]

This question already has an answer here:
Java Regex for telephone number - Must Include only 8 digits with not more than 2 dash [duplicate]
(1 answer)
Closed 7 years ago.
10 numeric digits, may be in the following formats: 123-4-567890, 1234-567890 or 1234567890
What is the regular expression for above digits?
Any help is appreciated.
Thanks.
Assuming you mean any digit, 0-9, should be found if (and only if) it meets the three formats you presented, one regex that would work is
(([0-9]{3}-[0-9]{1}-[0-9]{6})|([0-9]{4}-[0-9]{6})|([0-9]{10}))
The above breaks down to three separate patterns, one for each case you presented, separated by regex's equivalent of "or", the | character. Each of the statements above contains [0-9], a character class which will match any digit. Following each character class is a {n} statement, which means "repeat the previous item n times".
Disclaimer, there is probably a cleverer way to do this with a shorter pattern, but my regex-foo isn't quite that advanced yet