Home > front end >  Is there anyway I can avoid to match a 9 digit number from the following rules with the regex i have
Is there anyway I can avoid to match a 9 digit number from the following rules with the regex i have

Time:03-09

I have the below regex which follows the following rules,

(?<!x)(?=(?:[._ –-]*\d){9})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}

Rules:

  1. 9 digit Order numbers should not get detected if 'X or x' precedes the number. (WORKING FINE)
  2. 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.

  1. If the number beginning with the number “9”
  2. If the number “666” in positions 1 – 3.
  3. If the number “000” in positions 1 – 3.
  4. If the number “00” in positions 4 – 5.
  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| - 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 [._ –-]* .

CodePudding user response:

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
  • Related