Regex value of 0.0-5.0 [duplicate] - regex

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.

Related

Find the shortest match that matches a specific condition using regex [duplicate]

This question already has answers here:
Regular expressions: Ensuring b doesn't come between a and c
(4 answers)
Closed 4 years ago.
I'd like to find these three strings in any order and the result may have all these three strings including any character between them with the shortest length.
strings are: "ACT", "AGT" and "CGT".
Sample input: "ACTACGTTTAGTAACTCGTCT"
I tried but the regex returns the first occurrence matched which is "ACTACGTTTAGTAACTCGT"
/(ACT.*AGT.*CGT)|(ACT.*CGT.*AGT)|(AGT.*ACT.*CGT)|(AGT.*CGT.*ACT)|(CGT.*ACT.*AGT)|(CGT.*AGT.*ACT)/g
Output has to be "AGTACTCGT"
You can't return separate bits of a string already concatenated in one go.
See here: Regular expression to skip character in capture group
You can first match each bit, using parentheses to group them, and then put them together in a separate step

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 to match floats only [duplicate]

This question already has answers here:
Matching numbers with regular expressions — only digits and commas
(10 answers)
regular expression for finding decimal/float numbers?
(9 answers)
Closed 7 years ago.
I need to do a regular expression to match the floats only, what i got is the following :
[\-\+]?[0-9]*(\.[0-9]+)?
But this match also the below
123123132 ,
05/03/1994
I only need want to match the number with the decimal point
Your regex is almost correct for your purpose.
It finds 123123132, because the last part is optional. Removing the ? solves that.
[-+]?[0-9]*(\.[0-9]+)
With that adjustment, it might still find matches in strings like .12/39/3239, if you don't want that to happen, insert enforce matching over the complete string by inserting ^ and $:
^[-+]?[0-9]*(\.[0-9]+)$
How about:
([+-]?[0-9]*\.[0-9]*)
You can see it working here
Here is a regexp handling also existing exponents:
[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?
Debuggex Demo
Additionally you should force the hole string to be matched to avoid matchings within your date values.
^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
By the way here is a nice tutorial about matching floating point numbers using regular expressions: http://www.regular-expressions.info/floatingpoint.html.

RegEx to get numeric value and values with range [duplicate]

This question already has answers here:
How to write regex to match number ranges?
(7 answers)
Closed 8 years ago.
I need a RegEx to match the following format:
3
7.3
5.7 (2.2-10.4)
I want to check in JS if the given value i.e. 5.7 (2.2-10.4) is valid. And in PHP I want to split the string to get all three values (if there is a given range).
That means I have to check for a simple numeric value (int and float) or a numeric value followed by a range like (d-d)
My attempt: /^(\d+)\s\((\d+)\-(\d+)\)$/
based on your attempt, does not count for negative values
^(\d+(?:\.\d+)?)(?:\s*\((\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)\))?$
Demo

Regex for range 1-1000 [duplicate]

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.