This question already has answers here:
Regular Expression: Numeric range [duplicate]
(9 answers)
Using regular expressions to validate a numeric range
(11 answers)
Closed 8 years ago.
Can somebody help me with this regular expression?
Numbers that are anything from 100 to 9999. Excluding 112, 144 and the whole 900 - 999 range.
This is a great opportunity to not use regular expressions at all. You are interested in the value of the numbers not their textual format so just convert the value into an integer ( if the conversion fails you have bad input ) and then perform a numerical analysis on it.
This will be easier, more readable and very probably perform better than using a regular expression.
You can use this regex if regular script/language constructs don't work for you:
^(?!(9[0-9]{2}|112|144)$)[1-9][0-9]{2,3}$
Related
This question already has answers here:
Regex to allow decimal numbers greater than 0 and less than 100
(3 answers)
Regular Expression to accept all the decimals from 0 to 100
(7 answers)
how to match a number which is less than or equal to 100?
(7 answers)
Closed 4 years ago.
I'm trying to write a regular expression to validate a form field that is allowed to have a decimal data type, up to 8 digits long, that is greater than 0 but less than 100. The following seems to be close, although it permits zero and that is where I'm stuck. What should be modified to prevent zero from matching?
^([0-9]{1,2})(\.[0-9]{0,8})?$
Use a positive look behind
^([1-9]?(?<=[1-9])[0-9]?)(\.[0-9]{0,8})?$
learn more here https://www.regular-expressions.info/lookaround.html
and use this to play around with
https://regex101.com/
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.
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.
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
This question already has answers here:
Math operations in regex
(3 answers)
Closed 8 years ago.
i am wondering of it is possible to do some simple Math on RegEx Variable values.
E.G.:
I am looking for all two-digit numbers is a textfile and would like to multiply them by 10.
Is simple regex able to do this or do i need to use a more complex script for that?
thanks!
Multiply two-digits number is like appending 0 at the end of the numbers. So that can be done with any regular expression that support replace and capturing group.
For example, here is Python code:
>>> re.sub(r'\b(\d{2})\b', r'\g<1>0', 'There are 10 apples.')
'There are 100 apples.'
But what you want is multiply by arbitrary number, then you need the regular expression engine that support some kind of callback / evaluation.
>>> re.sub(r'\b(\d{2})\b', lambda m: str(int(m.group(1)) * 5), '10 apples.')
'50 apples.'