Regular expression for non negative integers including 0 - regex

How can I represent non negative integers including 0 and no integer, except 0 should start by a 0 using regular expression?
Example:
111 (true)|
0 (true)|
013 (false)|
120 (true)|
The regex I tried:
^(0|[1-9][0-9]?)$
This is how it's represented if 0 isn't included.

Try (regex101):
^(?!0\d+)\d+
Which evaluates:
111 - True
0 - True
013 - False
120 - True

You can change the quantifier from ? (which matches 0 or 1 times) to * which matches zero or more times.
Now the pattern matches either a single 0 or a digit that starts with 1-9 followed by optional digits 0-9.
^(?:0|[1-9][0-9]*)$
Regex demo
Or if a non capture group is not supported
^(0|[1-9][0-9]*)$

Related

Regex: to match unsigned integer values (valid values: 0 to 65535 only) for comma separated values

I have
([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])
which works for single input as:
0
1
65
6553
but i want them for comma separated input string as:
0,1,65,6553 ->this is a valid string
65535,-1,25 ->this is a invalid string because of negative number.
please can anyone suggest solution
Note:
I have already tried repetition as:
^([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])+(,(([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])))*$
which is accepting 65537 also which is undesirable.
Checking number bounds afterwards seems more straightforward to me, but anyway, this is a regex you may use (I refactored the integer part a little bit)
^(([0-5]?\d{0,4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(,|(?=$)))+$
https://regex101.com/r/1RpNuy/1
Details:
^ : String start
( : Group start
([0-5]?\d{0,4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]) : Match a number
(,|(?=$)) : Match either , or make sure this is end of line (but without reading the $)
)+ : End of group, repeat as many times as possible
$ : End of string
You may use
^(?:\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?:,(?:\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))*$
In PCRE and Onigmo, you may use a shorter pattern where \g<1> repeated Group 1 pattern:
^(\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?:,\g<1>)*$
See the regex demo and regex demo #2
The regex is basically ^<BLOCK>(?:,<BLOCK>)*$ where the BLOCK pattern is a regex matching the numbers from 0 to 65535:
\d{1,4} - 1, 2, 3 or 4 digits (0 - 9999)
[1-5]\d{4} - 1 to 5 digit and then any 4 digits (10000 - 50000)
6[0-4]\d{3} - 6, then a digit from 0 to 4, and then three digits (60000 - 64999)
65[0-4]\d{2} - 65, a digit from 0 to 4 and then any two digits (65000 - 65499)
655[0-2]\d - 655, a digit from 0 to 2 and then any digit (65500 - 655299)
6553[0-5] - 6553 and then a digit from 0 to 5 (65530 - 65535)
The general pattern:
^ - start of string
<BLOCK> - BLOCK pattern described above
(?:,<BLOCK>)* - 0 or more repetitions of , and then BLOCK pattern
$ - end of string.

Regular expression to get positive integer and -1

Below is the text I hope to match:
00000001,00000002,00000003
It works fine with ((([-1-9]+),)+)?[-1-9]+.
But it didn't match -1. The expression must not match with -2 or anything else except -1.
You may use
^(?:0*[1-9][0-9]*|-1)(?:,(?:0*[1-9][0-9]*|-1))*$
See the regex demo.
Pattern details:
^ - start of string
(?:0*[1-9][0-9]*|-1) - a non-capturing group matching...
0*[1-9][0-9]* - zero or mor 0 chars, followed with a non-zero digit followed with any 1 or more digits
| - or
-1 - a -1 substring
(?:,(?:0*[1-9][0-9]*|-1))* - a non-capturing group quantified with * (0 or more) quantifier matching 0 or more repetitions of:
, - a comma
(?:0*[1-9][0-9]*|-1) - same subpattern as in the beginning (-1 or a non-zero number with no fractions)
$ - end of string.
[-1-9]+ doesn't match what you're expecting it to match. It matches for example: "-31-23", which is obviously not a number.
A simple regex like:
(?:^-1)$|^[0-9]+
will match "-1", or any positive integer (including 0001, 00000002, etc...).
Also, depending on the language you're using, it would be simpler to use the language's features to decide if the number is "-1" or any other positive number.
As your state that ((([-1-9]+),)+)?[-1-9]+ works fine which captures a positive integer and looking at the title of the question, you might use this regex using alternation to capture -1 or only positive integers including 0 or 00000 from a string which could be preceded with zeroes.
The positive integers will be captured in group 1.
-[02-9][0-9]*|0*(-?[0-9]+)
Details
- Match literally
[02-9][0-9]* Match a 0 or digits 2-9 followed by zero or more times a digit. Note that the - is not part of the character class or else --- would also match.
| Or
0* Match zero or more times a zero
(-?[0-9]+) Capture in group 1 an optional hyphen followed by one or more times a digit

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

check if not 00000000 or 11111111 or 22222222, etc

I have problem with one regular expression to check if a (french) phone number is correct.
Phone number must start with one 0 continue with one 1 or 2 or 3 or 4 or 5 or 9 and continue with 8 numbers but theses numbers must not be the same like 00000000 or 11111111...
My current regular expression :
/^0(1|2|3|4|5|9){1,1}[0-9]{8,8}/i
Thanks in advance for help.
You can use the following regex:
/^0[1-59](?!(\d)\1{7}$)\d{8}$/i
Some points:
{1,1} is as good as being removed.
{8,8} is as good as {8}.
(1|2|3|4..) can be replaced with character class - [1234]
[12345] can be replaced by range in a character class - [1-5]
The above regex uses negative look-ahead assertion - (?!(\d)\1{7}) to assert that the 8 digits after first 2, are not all same. If the assertion is true, then it matches the next 8 digits.
(\d) captures the first digit in group 1
Then \1 backreferences the captured group to match the same digit that was matched by \d
{7} matches the backreference 7 times. That means - (\d)\1{7} matches same digit 8 times.