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$
Related
Below is my regex for matching 2 digit where tens place value is 2 or 3 and it is working fine.
^(?=[2,4])\d{1,2}$
As soon as I add the regex for matching single digit in above regex , It started matching single digit and as well all 2 digit number.
^(?=\d|[2,4])\d{1,2}$
I want below sample input to be matched.
0
1
2
3
24
44
48
29
28
Below not to be matched.
99
11
33
55
77
Also It will great help if I would get to know why my regex is not working.
You get a difference in matches as the positive lookahead asserts that there must be to the right what you specify. In there first pattern that is either 2 4 or , and in the second case just a single digit.
You don't have a comma in your example data, so in that case you can match an optional 2 or 4 using just [24]? followed by a digit without any lookarounds.
^[24]?\d$
See a regex demo.
Try this: ^(\d|[2,4]\d)$
Test regex here: https://regex101.com/r/aZo7fK/1
^(\d|[2,4]\d)$
^ matches the start of string
(\d|[2,4]\d) matches either a single digit(0-9) or a two digit number which starts with either 2 or 4
$ matches the end of the string
This matches either a single digit(0-9) number or a two digit number which starts with either 2 or 4.
I suggest
^[2,4]?[0-9]$
pattern; where
^ - anchor, start of the text
[2,4]? - optional 2 or 4 digit for tens
[0-9] - mandatory digit 0..9 for units
$ - anchor, end of the text
Edit: Now, let's have a look at your current patterns; the first is
^(?=[2,4])\d{1,2}$
Here
(?=[2,4]) - look ahead for 2 or 4
\d{1,2} - one or two digits
as we can see 3 doesn't match: look ahead fails to find 2 or 4. As for your second attempt
^(?=\d|[2,4])\d{1,2}$
pattern, where
(?=\d|[2,4]) - look ahead for ANY digit (note, that |[2,4] is redundant)
\d{1,2} - one or two digits
the pattern matches too many texts; technically it matches any one or two digit numbers, e.g. for:
79
we have
(?=\d|[2,4]) - look ahead - succeeds with 7
\d{1,2} - one or two digits - succeeds with 79
I have a regular expression like:
/^([0-9]{2,3})/
This will accepts 2 or 3 number digits between 0 and 9
123 or 12
I need a validate to: if the number has 3 digits, the first should be 0, in our case 023
and if not, number should be the 2 digits one: 12
Can anyone help me?
You may use
^0?\d{2}$
See the regex demo
Details
^ - start of string
0? - an optional 0
\d{2} - two digits
$ - end of string
I am trying to write some form validation, I need one of the inputs to be 13000-99999.
(^[1-1][3-3]?[0-9]?[0-9]?[0-9]?$|^[0-9][0-9][0-9][0-9][0-9]$)
It does not work as expected and it match all the following :
10 \\ matched but it should not
10000 \\ matched but it should not
12999
13000
20000
99999
can anyone help me? Thanks!
Although the way you are doing this is not ideal. But if you are doing with this approach, your regex needs some changes:
(^[1-1][3-9][0-9][0-9][0-9]$|^[2-9][0-9][0-9][0-9][0-9]$)
This is because if the 1st digit is a 1, then the second number should be between a 3 and a 9. If the 2nd digit is a 2, then any of 0-9 is valid for the second digit. The last three digits are always 0-9 range.
You have more than one issue here
(^[1-1][3-3]?[0-9]?[0-9]?[0-9]?$|^[0-9][0-9][0-9][0-9][0-9]$)
1 - You have to remove all question mark ?
2 - In the second pattern in the first part [3-3] should be from [3-9]
3 - In the second part after | this should be from [2-9]
You can use one of the following regex (^[1-1][3-9][0-9][0-9][0-9]$|^[2-9][0-9][0-9][0-9][0-9]$), (^[1-1][3-9]|^[2-9][0-9])[0-9][0-9][0-9]$ or 1[3-9]\d{3}|[2-9]\d{4}
first you need to match any number that start from 1 and from 3 to 9 or start from 2 to 9 and any number 0 to 9 and all rest number can any from 0 to 9
I want to write a regular expression on Google Form
First Character between 1 to 9
Second and Third any alphabets (Upper Case)
and next 3 characters should be number like 541 or 001 but not 000
This expression is also taking 000
[1-9][A-Z]{2}[0-9]{3}
Use alternations:
[1-9][A-Z]{2}([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])
See regex demo
Here,
[1-9] - matches 1 digit from 1 to 9
[A-Z]{2} - two uppercase ASCII letters
([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9]) - 3 alternatives:
[1-9][0-9][0-9] - 3-digit numbers starting with 1
[0-9][1-9][0-9] - 3-digit numbers having 1 in the middle
[0-9][0-9][1-9] - 3-digit numbers ending with 1
Also, see this regex demo.
Use a negative look-ahead to avoid the triple zero at the end:
[1-9][A-Z]{2}(?!000)[0-9]{3}
Using the alternation operator
[1-9][1-9][1-9]|0[1-9][1-9]|00[1-9]|0[1-9]0
^((?=.*[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!