0

I want to match an 8 digit number. Currently, I have the following regex but It is failing in some cases.

(\d+)\1{6}

It matches only when a number is different at the end such as 44444445 or 54444444. However, I am looking to match cases where at least 7 digits are the same regardless of their position. It is failing in cases like

44454444
44544444
44444544

What modification is needed here?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Waqar ul islam
  • 418
  • 4
  • 17
  • why would you want to do that by regex. Performance would be terrible because it'll need to backtrack a lot – phuclv Aug 03 '21 at 01:42

3 Answers3

1

You can sort the digits before matching

string input = "44444445 54444444 44454444 44544444 44444544";
string[] numbers = input.Split(' ');
foreach (var number in numbers)
{
    number = String.Concat(str.OrderBy(c => c));
    if (Regex.IsMatch(number, @"(\d+)\1{6}"))
        // do something
}

Still not a good idea to use regex for this though

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

It's probably a bad idea to use this in a performance-sensitive location, but you can use a capture reference to achieve this.

The Regex you need is as follows:

(\d)(?:.*?\1){6}

Breaking it down:

  • (\d) Capture group of any single digit
  • .*? means match any character, zero or more times, lazily
  • \1 means match the first capture group
  • We enclose that in a non-capturing group {?:
  • And add a quantifier {6} to match six times
Charlieface
  • 52,284
  • 6
  • 19
  • 43
0

The pattern that you tried (\d+)\1{6} matches 6 of the same digits in a row. If you want to stretch the match over multiple same digits, you have to match optional digits in between.

Note that in .NET \d matches more digits than 0-9 only.

If you want to match only digits 0-9 using C# without matching other characters in between the digits:

([0-9])(?:[0-9]*?\1){6}

The pattern matches:

  • ([0-9]) Capture group 1
  • (?: Non capture group
    • [0-9]*?\1 Match optional digits 0-9 and a backreference to group 1
  • ){6} Close non capture group and repeat 6 times

See a .NET Regex demo


If you want to match only 8 digits, you can use a positive lookahead (?= to assert 8 digits and word boundaries \b

\b(?=\d{8}\b)[0-9]*([0-9])(?:[0-9]*?\1){6}\d*\b

See another .NET Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70