Kirik Andrey
Kirik Andrey

Reputation: 23

Match repeated digits from number

I need regex to check numbers for repeated digits.

All numbers contain 12 digits, first 6 digits we need to skip, so I need to find numbers where every second digit from 7 repeated.

Like this 964632X5X7X3 X - repeated digits

Results

502632959793 - TRUE
125632757773 - TRUE
475632353773 - FALSE

I have try something like this for every digits from 0 to 9:

\d{6}([9]\d[9]\d[9]\d)$

It didnt work.

Upvotes: 2

Views: 73

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

You may use

^\d{6}(?=(\d))(?:\1\d){3}$

See the regex demo. You may even refactor this regex later if you need to accommodate any x to y amount of repetitions after the first six digits (just replace {3} with the required {x}, {x,} or {x,y} quantifier with the required thresholds).

Regex details

  • ^ - start of string
  • \d{6} - the first six digits
  • (?=(\d)) - a positive lookahead that captures the seventh digit into Group 1
  • (?:\1\d){3} - three occurrences of the digit captured in Group 1 and any single digit
  • $ - end of string

Upvotes: 1

Related Questions