Regex to match specific number format - regex

I am having trouble figuring out how to write a regex to match a number (technically a string) with the following rules:
all numeric
must be exactly 11 digits
it must start with at least 2 zeros
it may not start with more than 4 zeros
I can use \d{11} to match for the exactly 11 digits, and ^0{2,4] to match the leading zeros part, but I can't figure out how to combine them.

^00(?!000)\d{9}$
It checks for two zeroes and then checks that there are not more than 2 0's following it.
if it is not it checks the other 9 numbers to the end of the string.
This assumes your engine supports lookaheads.

Related

Regex with constraints, look-ahead function

I'm trying to write a regex expression between 10 and 12 digits. There will be optional leading 0 (zeros) between {0,5} then a numeric string between 10-12 digits. Regardless of the number of zeros (0 to 5), I want 10-12 digits after leading zeros
Example:
0000012345 should not be passing
0012345678 should not be passing as there are only 8 digits after leading zeros
I've tried:
^(0{0,5}(?=\d{10,12}$)^\d{1,2}?\s?(\d{10})$
I think
^0{0,5}[1-9]\d{9,11}$
should be what you need. It enforces not counting the leading zeroes as one of the later digits by requiring it be non-zero. Then there can be 9-11 other digits (including 0).
If you need to include an optional space at any point (as suggested by your RegEx), the RegEx would grow a lot, and it might be easier to do this with some additional code. However, if you give the exact requirements, I will edit the answer accordingly.
^0{0,5}+\d{0,2}\s?\d{10}$
^^
You didn't specify the language. You need "possessive quantifier" here.
See demo:
https://regex101.com/r/m5sOAJ/1
Or if your regex does not support possessive quantifiers:
^(?=(0{0,5}))\1\d{0,2}\s?\d{10}$
See demo:
https://regex101.com/r/m5sOAJ/3

Regex to allow one special character with at least 5 digits and maximum 6 digits

I have created a regex which follows the following parameters:
Minimum length: 5
Maximum length: 6
Needs to have at least 5 digits
Space and Special characters allowed: #&()_+[]:;',/.\-"*
No alphabets allowed
The regex I created is :
^\d{3}[_\+\[\]\:\;\'\,\/.\-"!##$%^&*()\s]{0,1}\d{2,3}$
This is fulfilling the length requirements and 5 digit requirement, however it is not allowing special characters. I am blocked due to this and unable to find any solution, please help.
You could do it with
^(?:(?=.{6}$)\d*[-#&()_+[\]:;',\/.\\"*]\d*|\d{5,6})$
if your regex-flavor supports look-aheads.
It uses two alternations. The first starts by checking the length, which including a special character always must be 6 (to allow for 5 digits), with a positive look-ahead. Then it matches any number of digits, followed by a special character, and finally any number of digits.
The other alternative just checks for 5-6 digits.
See it here at regex101.

Powershell Regular Expression

In Powershell, how do I specify a regular expression consisting of one, two or three digits, followed by a decimal point and one or two more digits, for use in a -match comparison?
[0-9][0-9]*[0-9]*\.[0-9][0-9]* is the closest I can come up with, but this allows more digits than I want. I can't find any way to limit a term to a maximum number of repetitions.
You can try use the following: \d{1,3}\.\d{1,2}. The numbers within the braces denote the minimum and maximum amount of repetitions which the regex engine will match. An example is available here.
The issue with your expression is that your are using the *. This means 0 or more repetitions of, thus, [0-9]* would fit an empty string, a string with 1 digit, 2 digits, and so on.
An alternative (and closer to what you did) would be to use the ? operator instead, which means 0 or 1 instances of, thus this: [0-9][0-9]*[0-9]*\.[0-9][0-9]* would become this: [0-9][0-9]?[0-9]?\.[0-9][0-9]?, which will match a digit followed by, optionally at most 2 more numbers, followed by a period, by a digit and optionally 1 more digit.

Regular Expression (consecutive 1s and 0s)

Hey I'm supposed to develop a regular expression for a binary string that has no consecutive 0s and no consecutive 1s. However this question is proving quite tricky. I'm not quite sure how to approach it as is.
If anyone could help that'd be great! This is new to me.
You're basically looking for alternating digits, the string:
...01010101010101...
but one that doesn't go infinitely in either direction.
That would be an optional 0 followed by any number of 10 sets followed by an optional 1:
^0?(10)*1?$
The (10)* (group) gives you as many of the alternating digits as you need and the optional edge characters allow you to start/stop with a half-group.
Keep in mind that also allows an empty string which may not be what you want, though you could argue that's still a binary string with no consecutive identical digits. If you need it to have a length of at least one, you can do that with a more complicated "or" regex like:
^(0(10)*1?)|(1(01)*0?)$
which makes the first digit (either 1 or 0) non-optional and adjusts the following sequences accordingly for the two cases.
But a simpler solution may be better if it's allowed - just ensure it has a length greater than zero before doing the regex check.

Regular expression to accept 00100 to 99999 with the length of 5 Characters

As mentioned in Question how can I write a regular expression to accept from 00100 to 99999.
I have written
\d{5}
But it will accept all zeros also.
Alternatively you can just disallow the numbers that you don't want to include using negative look ahead assertions:
^(?!000)\d{5}$
Which does not match any five digit number that has three zeroes at the beginning which is also what you want.
See it
Try this:
^00[1-9][0-9]{2}$|^0[1-9][0-9]{3}$|^[1-9][0-9]{4}$
See it working online: rubular
Note that regular expressions are not a good tool for accepting arbitraty integer ranges. It may be easier to read if you convert your string to an integer and use ordinary code to test that the integer is in range.
[0-9]{2}[1-9]{1}[0-9]{2}
This reads like this:
[0-9]{2} I want exactly 2 digits....you could have used \d{2} here.
[1-9]{1} I want exactly 1 digit in the 1-9 range. The {1} is unneeded, but I think helps with clarity.
[0-9]{2} I want exactly 2 digits again.
Should give you exactly what you want.