RegEx for number with in range 1 to 100000 with decimal upto two digits.
-1, 0 should not be allowed.
1 to 100 000 allowed e.g. 1.00, 13.99, 100.45,9999.34, 99999.99, 100 000
use this :
^(?:[1-9][0-9]{0,4}(?:\.\d{1,2})?|100000|100000.00)$
demo here : http://regex101.com/r/bY1yT2/2
You could try the below regex to match the numbers from 1 to 100000 with optional decimal upto two digits.
^(?:100000|[1-9]\d{0,4})(?:\.\d{1,2})?$
DEMO
\d{0,4} Matches upto four digits.
(?:\.\d{1,2})? Matches an Optional decimal and the following digits. Number of digits following must be 1 or 2.
Related
I am after a regex that allows a number to be a maximum length with decimal places.
It should allow 16 numbers maximum and 3 decimal places, however the 16 max should include the decimals but not the . character. I've tried and have this so far: ^(?=^[\d\.].{0,16}$)[0-9]+(\.[0-9]{1,3})?$, which is close except it allows 17 single numbers, when it should only accept 16. If I change {0,16} to {0,15} it then breaks the decimals.
Accepted values:
123
123.3
123.45
123.456
1111111111111.345 (totally 16 numbers)
11111111111111.45 (totally 16 numbers)
Rejected:
123.1234
11111111111111111 (17 characters)
1111111111111111.
111111111111111.56 (15 characters and 2 numbers = 17)
Thanks in advance.
Your regex is fairly close, you may use this regex:
^(?=(?:\d\.?){0,16}$)\d+(?:\.\d{1,3})?$
RegEx Demo
RegEx Details:
(?=(?:\d\.?){0,16}$): is positive lookahead to assert that we have 0 to 16 digits in input where dot is optional.
\d+(?:\.\d{1,3})?: Match an integer or a decimal number with 1 to 3 digits.
I am trying to get regex which can validate a) accepted numbers should be between 0 to 20 and b) these numbers can have 1 or 2 decimal numbers.
RegEx:
^(10|\d)(\.\d{1,2})?$
(10|\d) allows for a single digit or 10
(\.\d{1,2})? possible decimal followed by 1 or 2 digits
This gives me desired result from 0 to 10 (http://regexr.com/). I am not sure what needs to modify if I want it to accept from 0 to 20.
Valid:
0
1
2 or 2.5
20
Invalid:
21
.1
.0345
This is what I have tried:
1. ^\d{0,2}$(\.\d{1,2})?$
- Accepts 0 to 99 but no decimal
2. ^([1-9]|20)$
- Accepts 1 to 9 only
You can use this regex:
^(?:1?\d(?:\.\d{1,2})?|20(?:\.0?0?)?)$
RegEx Demo
(?:1?\d) - will match number in the range 0-19
(?:\.\d{1,2})? - will allow optional 1 or 2 decimal numbers.
20(?:\.0?0?)? - will match 20 with optional zeroes after decimal point.
I'm looking for a regex to validate if numbers are below 20 000.
I can't find the right solution, I have so far this:
(^([1-9]([0-9]{0,3})|20000)$)
Which works quite ok but as soon as it gets to 10 000 it gives no matches. So I have a gap from 9 999 - 20 000.
What am I doing wrong? I don't use regex for these situations, but the 3th party program required regex for such..
Thanks!
Your regex - ^([1-9]([0-9]{0,3})|20000)$ - matches numbers from 1 till 9999 and 20000.
You may use
^([1-9]\d{0,3}|1\d{4}|20000)$
See demo
Breakdown:
^ - match start of string
([1-9]\d{0,3}|1\d{4}|20000) - match one of the alternatives:
[1-9]\d{0,3} - 1 to 9 followed with 0 to 3 any digits (from 1 till 9999)
1\d{4} - 1 followed with any 4 digits (to match 10000 - 19999)
20000 - literally 20000
$ - match the end of string
I've got this:
^([01]?\d{0,4}|20000)$
Which match any number from 0 to 20 000 and allow the user to use number with leading 0 Live Demo
The ([1-9]([0-9]{0,3}) part is designed to match all numbers strictly below 2000 but you define it as: "A digit one to nine followed by zero to three digits". Now 10 000 is a one followed by four zeros: you can rewrite the part as:
[1-9][0-9]{3}|1[0-9]{4}
The full regex is now:
^[1-9][0-9]{3}|1[0-9]{4}|20000$
^((?=.*[1-9]|0)(?:\d{1,3}))((?=.*\d)(?:\.\d{3})?)*((?=.*\d)(?:\,\d\d){1}?){0,1}$
I actually think this regular expression is very long, and mayby could be shorter. The problem is i'm not very good with regular expressions and therefore I ask you for help.
Online regex tester http://regexr.com/3a3mk
My rules:
Starting with 1, 2 or 3 positive numbers [1-9] or 0.
Adding as many . (followed by 3 numbers [0-9]) as you want.
Possibility to add a comma with 2 numbers (as decimals)
Positive results
0
0,55
1
1,60
10
10,70
100
100,80
1,10
1.000
1.000,20
10.000
10.000,03
100.000
100.000,08
1.000.000.000
1.000.000.000,10
Negative results
0,0
1,1
1,000
1000.000
0.000
0.000,10
1.000,1
1.000,100
1.0,00
1.00,00
1.000,0
01
012,10
012.123,10
a
a0
0,a
0,aa
1.a00.00
1.000.a1
[EDIT] Added more negative results
The following should suit your needs:
^(?:0|[1-9]\d{0,2})(?:\.\d{3})*(?:,\d{2})?$
Visualization by Debuggex
Demo on regex101
Edited:
^(0|[1-9][0-9]{0,2}(\.[0-9]{3})*)(,[0-9]{2})?$
matches:
^ beginning of line
[1-9] just one non-zero digit
[0-9]{0,2} between 0 and 2 digits
(\.[0-9]{3})* zero or more lots of a period and 3 digits
(0 | [1-9][0-9]{0,2}(\.[0-9]{3})*) either (i) a zero or (ii) up to three digits (the first not a zero) followed by blocks of zero or more lots of a period followed by three digits
(,[0-9]{2})? zero or one lots of a comma and 2 digits
$ end of line
You'Re right, that your expression is a bit to long. A shorter version that works with the example numbers and specifications you gave would be this:
^(0|\d{1,3})(\.\d{3})*(,\d{2})?$
Explanation:
(0|\d{1,3}) checks for a 0 or 1 to 3 digits
(\.\d{3})*checks for a dot and 3 numbers, but because of *there can also be none of them
(,\d{2})? ckecks for a comma and two digits, but again it can appear once or not at all.
Hope it helps you!
Can a Regex be written to find numbers in a string that are greater than x?
Say for example x = 1800
Can we find for that a number > 1800 which is in the strings: "3,000.00/month" | "$2800" | "150,000.00a month" | "only $1900" etc? The regex would find 3,000.00, 2800, 150,000.00 and 1900 because they are all greater than 1899.
What is this pattern that elludes me..
You can find any well-formatted number greater than a number with a regular expression, but it does get more complicated as the numbers get bigger.
Let's start with a simple example where x = 11, and you wanted to find any number in a string greater than 11. The regex you'd write would follow a pattern similar to the solution below:
(\d{3,}|[2-9]\d|1[2-9])(\.\d+)?|11\.\d*[1-9]\d*
REY
What is the purpose of first three inner alternations? The first captures any three or more digit number, the second any two digit number where the digit in the 10's place is 2-9, and the last any two digit number where the digit in the 10's place is 1 and the digit in the 1's place is 2-9. Then it optionally captures any decimal decimal digits.
The second outer alternation actually matches x in the integer part, but then checks the decimal part to ensure it is somehow greater. If you had a decimal part of 12 for x then you'd just do 120*\d*|1[3-9]\d*|2\d*.
There are two situations that will make the above pattern not work:
When a number contains commas
When a number starts with zeroes.
The example below deals with those cases as well and solves for x = 1800.
#FIND any number > 1800
(?=[1-9]) # ensure number doesn't begin with a zero
(
(\d{1,3},(?=\d{3}))*\d{2,3},\d{3}| # any number that has >= 5 digits with commas
\d{5,}| # any number that has >= 5 digits without commas
[2-9],?\d{3,}| # any 4 digit number that starts with 2-9
1,?9\d{2}| # any 4 digit number that starts with 1 then 2-9
1,?8[1-9]\d| # any 4 digit number that starts with 18 then 1-9
1,?80[1-9] # any 4 digit number that starts with 180 then 1-9
)
(\.\d+)? # any decimal digits
|1,?800\.\d*[1-9]\d* # any number whoses integer = 1800,
# ... then has a decimal with a non-zero digit.
REY
At the top of the regex is uses a look ahead to ensure a number actually starts with 1-9. Without it a number like 00005 would be confused as a five digit number.
The inner group finds all integer matches greater than 1800, just like the first example tried to match numbers greater than eleven. Probably, the only non obvious alternation is the first - (\d{1,3},(?=\d{3}))*\d{2,3},\d{3}. To find a 5+ digit number it matches 1-3 digits followed by a comma, then 2-3 digits, a comma and then the last 3 digits. Without the look ahead it would incorrectly match a non-number such as 234,23,412.
If you're going to be picky about commas, like I am being, then you're not going to be able to integrate comma and non-comma cases such as with 1,?9\d{2} => 1914 & 1,914. Having multiple ,? would lead to incorrect matches such as 1000,050, when trying to find a number greater than a million.
You could solve it in two steps: first find a number and then apply a regex to it that checks if it is greater than you number x.
As an example:
here is a regex that matches numbers from 31 to 99999, which you can adjust to your needs:
^(?:[3][1-9]|[4-9][0-9]|(^[1-9][0-9]{2,4}$))$
where
[3][1-9] - matches numbers from 31 to 39
[4-9][0-9] - matches numbers from 40 to 99
^[1-9][0-9]{2,4} - matches numbers from 100 to 9999
The last bit ^[1-9][0-9]{2,4} can be changed to ^[1-9][0-9]{2,} to match any number greater than 100