0

I need a regex pattern that matches any number that it's non-repeated digits are more than 7 digits. for example it has to match:

1234567
122345678

But not to match:

1222345

non-repeating digits in this number for example 122345678 is 8 while the whole number has 9. but the non-repeating digits in this number: 11111222345 is only 5. this pattern doesn't do it:

/[0-9]{7,}/

I want it to count the non-repeating digits, not to ignore any number that has repeating digits.

Toto
  • 89,455
  • 62
  • 89
  • 125

1 Answers1

1

How I understand your question is, that you want to extract numbers composed of at least 7 different digits. Would use preg_match_all() to get the numbers in combination with a simple non-regex check:

if(preg_match_all('~\d{7,}~', $str, $out) > 0)
{
  $res = array_filter($out[0], function($v) {
    return count(array_unique(str_split($v))) >= 7;
  });
}

See this demo at 3v4l.org

The word "repeating" surely lead to confusion. Also "more than 7 digits" which would be at least 8.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • array_unique removes duplicates and keep 1 element. Try with `$str = '122334455667789';` it should be invalid because there is only 3 non duplicate (i.e. 1, 8, 9) but your code gives valid with 9 unique – Toto May 25 '19 at 12:03
  • @Toto I think OP wants the unique digits of number to be at least 7. So in your example `122334455667789` there is 9 different digits which is more than 7. – bobble bubble May 25 '19 at 12:13
  • 1
    My previous comment has been deleted. I said: you're probably right – Toto May 25 '19 at 14:26