regex - test for exactly 1 number and exactly 2 letters - regex

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

Related

Regular expression 2 digits 10 letters/digits

I'm trying to make a regular expression, but something isn't working for me, the requirements are the following:
Min length is 1
Max length is 12
The first 2 symbols must be numbers
Next 10 must be either letters or numbers
This is what I have so far
/^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$/
Can you guys tell me what I'm doing wrong?
Your pattern ^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$ matches 0, 1 or 2 digits at the start.
Then it matches 2 chars [a-z][A-Z] being a lowercase and an uppercase char A-Z which should be present in the string, and also makes the string length at least 2 chars.
You can make the second digit optional, and use 1 character class for the letters or numbers.
The length then has a minumum of 1, and a maximum of 12.
^(?!\d[a-zA-Z])\d\d?[a-zA-Z0-9]{0,10}$
^ Start of string
(?!\d[a-zA-Z]) negative lookahead, assert not a digit followed by a-zA-Z
\d\d? Match 1 or 2 digits
[a-zA-Z0-9]{0,10} Match 0-10 repetitions of any of the listed ranges
$ End of string
Regex demo
Or a version withtout a lookahead as suggested by #Scratte in the comments, matching a single digit and optionally a second digit followed by 0-10 repetitions of the listed ranges:
^\d(?:\d[A-Za-z\d]{0,10})?$
Regex demo

Match repeated digits from number

I need regex to check numbers for repeated digits.
All numbers contain 12 digits, first 6 digits we need to skip, so I need to find numbers where every second digit from 7 repeated.
Like this 964632X5X7X3 X - repeated digits
Results
502632959793 - TRUE
125632757773 - TRUE
475632353773 - FALSE
I have try something like this for every digits from 0 to 9:
\d{6}([9]\d[9]\d[9]\d)$
It didnt work.
You may use
^\d{6}(?=(\d))(?:\1\d){3}$
See the regex demo. You may even refactor this regex later if you need to accommodate any x to y amount of repetitions after the first six digits (just replace {3} with the required {x}, {x,} or {x,y} quantifier with the required thresholds).
Regex details
^ - start of string
\d{6} - the first six digits
(?=(\d)) - a positive lookahead that captures the seventh digit into Group 1
(?:\1\d){3} - three occurrences of the digit captured in Group 1 and any single digit
$ - end of string

Iranian postal code validation

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

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.