Iranian postal code validation - regex

Please, I need to validate Iranian postal code using regex.
I write this regex for this case \b([^02\n\D]){4}[^5](\d){5} but its not working on rule number 5 and 7.
please help me to fix it.
this is some rules about this regex:
It's all numeric
10 digit count
don't use 0 in first 5 digit
don't use 2 in postal code
First 4 digit is not the same
The 5th digit cannot be 5
all digits aren't the same

The following regex satisifes your conditions:
\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b
Click for Demo
Explanation:
\b - a word boundary
(?!(\d)\1{3}) - negative lookahead to make sure that the 1st 4 digits are not the same.
[13-9]{4} - matches 4 occurrences of all the digits except 0 and 2
[1346-9] - matches a single digit that is not a 0,2 or 5
[013-9]{5} - matches 5 occurrences of all the digits except 2
\b - a word boundary

Related

regex - test for exactly 1 number and exactly 2 letters

Please assist me with creating this regex.
A total/exactly 3 digit alphanumeric
Exactly 1 numeric excluding 0 and 1 2-9
Exactly 2 Alpha excluding letter O and L - o and l
Number can be in any position
Valid codes:
A2M
HH9
3AM
Invalid Codes
10M (too many digits and invalid digits,
22A (two many digits),
MAB (missing digit)
MA2M (too long, not length of 3)
thank you for all the help. Here is the regex I will use, this one removes the letter L and lowercase l:
/([2-9]{1}[A-KMNP-Za-kmnp-z]{2}|[A-KMNP-Za-kmnp-z]{1}[2-9]{1}[A-KMNP-Za-kmnp-z]{1}|[A-KMNP-Za-kmnp-z]{2}[2-9]{1})/g
You may be able to shorten your regex with use of a lookahead:
^(?=\D*\d\D*$)[A-KMNP-Za-kmnp-z2-9]{3}$
RegEx Demo
RegEx Details:
^: Start
(?=\D*\d\D*$): Lookahead to ensure that we have exactly one digit
[A-KMNP-Za-kmnp-z2-9]{3}: Match any letter or digit except [01LlOo] exactly 3 times
$: End

Regex limiting a number string

I am trying to figure out how to use regex to pass a 6 digit number string. My trouble is the string can be any 6 digits, unless it starts with 12. So the first digit can be 1 but not if second digit is 2. The second digit can be 2, but not if the first is 1.
I tried this, ([^1])([^2])(\d{4}) but that does not take into account both digits, so it will block anything with a 2 in the second spot.
Thank you for any help.
You may use
^([02-9][0-9]|[0-9][013-9])[0-9]{4}$
See the regex demo
Details:
^ - start of string
([02-9][0-9]|[0-9][013-9]) - either of the two alternatives:
[02-9][0-9] - any digit but 1 and then any digit
| - or
[0-9][013-9] - any digit and then any digit but 2
[0-9]{4} - any 4 digits
$ - end of string.
Another way is to use a negative lookahead:
^(?!12)[0-9]{6}$
See another demo. Here, (?!12) fails the match if the first 2 digits are 12. The [0-9]{6} will match 6 digits.
Depending on the regex library/method, ^/$ anchors may not be required. Lookaheads are not always supported, too.

Regex that matches any 9 digits except last 4 digits can't be 9999 or 0000

I am trying to figure out a regular expression that matches any 9 digits, but the last 4 digits can't be 9999 or 0000. For example, I want the regex to match these:
123456789
123459991
123459990
But not these:
123450000
123459999
I tried negative lookahead. But it doesn't seem to fit in my requirement.
The closest I can get is \d{5}[^\D90]{4}, but with this the last 4 digits can't be 0 or 9 at all, which is not what I want.
It is a special zip code requirement. Any help will be appreciated!
use a negative lookahead after the 5th digit:
^[0-9]{5}(?!0000|9999)[0-9]{4}$
or a lookbehind at the end:
^[0-9]{9}$(?<!0000|9999)
(if your regex flavor doesn't allow a lookbehind with an alternation, use two lookbehinds:
^[0-9]{9}$(?<!0000)(?<!9999)
Your \d{5}[^\D90]{4} regex matches any 5 digits followed by 4 characters other than a non-digit, 9 and 0.
You can use
^(?!\d*(?:9999|0000)$)\d{9}$
Shorter variant: ^(?!\d*(?:9{4}|0{4})$)\d{9}$. See the regex demo
The negative lookahead (anchored at the start) will fail the match if the input contains some digits and ends with either 9999 or 0000.
If we go on optimizing the regex, the most efficient version (basing on what Casimir suggests in his answer) is:
^\d{5}(?!9{4}|0{4})\d{4}$
See the regex demo
Here,
^ - start of the string
\d{5} - exactly 5 digits
(?!9{4}|0{4}) - check (but not match, the index stays after the 5th digit since it is a zero width assertion) if there are exactly 4 9s or 0, and if found, the match is failed (as (?!...) is a negative lookahead)
\d{4} - exactly 4 digits
$ - end of string.

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

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.