Home > Enterprise >  Regex to Match Repetitive Digits
Regex to Match Repetitive Digits

Time:10-13

I have a regex to match a phone number only if it ends with 3 similar numbers. Like 550268000.

^5(\d)(?!\1)(\d)(?!\1|\2)(\d)(?!\1|\2|\3)(\d)(?!\1|\2|\4)(\d)(\d)\6{2}$

The problem with my regex is that it won't match the phone number if it has another number that is randomly repeated 3 times in different indexes. Like 550568000.

I want my regex to match the digit if it ends with three similar numbers despite having random repetition of another digit.

CodePudding user response:

You may use this regex:

^5\d*(\d)\1{2}$

RegEx Demo

RegEx Breakdown:

  • ^: Start
  • 5: Match digit 5
  • \d*: Match 0 or more digits
  • (\d): Match a digit and capture in group #1
  • \1{2}: Match 2 repetitions of captured value in group #1
  • $: End
  • Related