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
CodePudding user response:
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|
- a9
digit, or66\D*6|
-66
, zero or more non-digits,6
, or00\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 a0
, 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 a0
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 [._ –-]*
.
CodePudding user response:
You may use this regex:
(?<![xX])(?=(?:[._ –-]*\d){9})(?!9|666|000|.{3}00|.{5}0000)\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
To enforce all rule we have a negative lookahead:
(?!9|666|000|.{3}00|.{5}0000)
That does following:
9
: Doesn't start with9
666
: Doesn't start with666
000
: Doesn't start with000
.{3}00
: Doesn't allow00
in position 4-5.{5}0000
: Doesn't allow0000
in position 6-9