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.
Related
I have the below regex which follows the following rules,
(?<!x)(?=(?:[._ –-]*\d){9})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
Rules:
9 digit Order numbers should not get detected if 'X or x' precedes
the number. (WORKING FINE)
9 digit numbers, Non-numeric characters or whitespaces (up to 3) in
between numbers should also get matched. (WORKING FINE)
Below is regex demo which shows it matches the numbers with the above rules.
https://regex101.com/r/mrGcvp/1
Now, the regex pattern should not match the 9 digit numbers following the above rules if it comes under the below rules of exclusion.
Rules of exclusion,
The number should not be matched at all for the following rules.
If the number beginning with the number “9”
If the number “666” in positions 1 – 3.
If the number “000” in positions 1 – 3.
If the number “00” in positions 4 – 5.
if the number “0000” in positions 6 – 9
You can use
(?<!x)(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
See the regex demo.
The added part is (?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3}) and it fails the match if, immediately to the right of the current location, right after the (?<!x) and (?=(?:[._ –-]*\d){9}) checks, there is
9| - a 9 digit, or
66\D*6| - 66, zero or more non-digits, 6, or
00\D*0| - 00, zero or more non-digits, 0, or
(?:\d\D*){3}0\D*0| - three occurrences of a digit and then zero or more non-digits, and then a 0, zero or more non-digits, 0, or
(?:\d\D*){5}0(?:\D*0){3}) - five occurrences of a digit and zero or more non-digits, 0, and then three occurrences of zero or more non-digits followed with a 0 char.
Note I used \D* instead of [._ –-]* that should be enough here, but if you want to make it more precise, you may replace each \D* with [._ –-]* .
You may use this regex:
(?<![xX])(?=(?:[._ –-]*\d){9})(?!9|666|000|.{3}00|.{5}0000)\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
RegEx Demo
To enforce all rule we have a negative lookahead:
(?!9|666|000|.{3}00|.{5}0000)
That does following:
9: Doesn't start with 9
666: Doesn't start with 666
000: Doesn't start with 000
.{3}00: Doesn't allow 00 in position 4-5
.{5}0000: Doesn't allow 0000 in position 6-9
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
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
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.
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