First number of regex should be 0 (validate) - regex

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

Related

Regex Match range numbers between 13000 to 99999

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

Regex, numbers below 20k

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$

Regular Expression for 3 digit without 000

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

How to match a whole string that contains just two and no more than two digits between 0 and 10 in regex?

This regex does not work for me as selects all groups of two and multiple digits and not the string.
abcde9 = match
abcde12 = not matched
abcde12345678 = not matched
What I have at the moment is this, it I just can't include the 0 and the 10 as two digits numbers in the regex, can anyone help me?
\d{0,10}[1-9]
If you want to match any string containing exactly one integer from 0 to 10 then use
^\D*(\d|10)\D*$
which means "any non-digit content followed by either a single digit or the number 10 and then followed by any non-digit content"
try it at regex101
I think you are looking for
^\D*(?:[0-9]|10)(?:\D+(?:[0-9]|10))?\D*$
See demo
This will match a whole string that contains 1 or 2 whole integer numbers from 0 to 10, and no other digits.
The regex breakdown:
^ - start of string
\D* - 0 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
(?:\D+(?:[0-9]|10))? - 1 or 0 occurrence of
\D+ - 1 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
\D* - 0 or more characters other than digit
$ - end of string
Is that what you looking for:
/(0[1-9])$/
You can test that regex to make sure it fits your needs:
https://regex101.com/r/hX6lB7/3

Regex not working as expcted

I need help with the below regex:
(0 ?)([1-8] ?){1}(\d ?){9}|(\d ?){10}
The regex should match either 10 or 11 digits
The first digit should be 0
The second digit should contain from 1 - 8 digits only
All digits can have spaces in between e.g. 0 1 2 7 4 3 3 3 4 4 4
Digits can be without spaces e.g. 01274333444
The regex I created works for most of the scenarios apart from the third condition i.e. The second digit should contain from 1 - 8 digits only.
Any help very much appreciated
Here is what your regex (0 ?)([1-8] ?){1}(\d ?){9}|(\d ?){10} is currently doing:
It's also trying to match 11 or 12 digits instead of 10 or 11.
What you need to do is change it one of the following ways:
Add a group around the OR | to limit it's scope: (0 ?)([1-8] ?){1}(?:(\d ?){8}|(\d ?){9})
Or preferably change it to a 8 or 9 character match: (0 ?)([1-8] ?){1}(\d ?){8,9}
You can use this:
^0 ?[1-8] ?(?:[0-9] ?){8,9}$
I used anchors ^ and $ to ensure that there is no leading or trailing digits.
Try this one:
^(0\s?)([1-8]\s?)(\d\s?){8,9}$