1

Need help to stop user from entering phone number with three or more repeated digits except 999.

Phone number could be 10 digit to 25 digit.

Bad Input:

  • 8777267331
  • 000
  • 66667878763

Exception:

  • 767886655999

I tried --> \b(\d)\3+\b but looks that is not right.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
Avatar Girase
  • 133
  • 2
  • 6

2 Answers2

4

Have a try with

^(?:([0-8])(?!\1\1)|9){10,25}$

See demo at regex101

Or something like this

^(?!.*?([0-8])\1\1)\d{10,25}$

Another demo


Digits are captured in first group. A negative lookahead is used to check for backrefernce \1.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Thank You @bobble bubble. The second one helps. what if instead 0f 999 the allowed digits are 888 – Avatar Girase Jun 25 '22 at 02:29
  • 1
    @AvatarGirase Welcome:) For `888` allowed like this: [`^(?!.*?([0-79])\1\1)\d{10,25}$`](https://regex101.com/r/VYA1uI/1) or first one: [`^(?:(\d)(?!\1\1)|8){10,25}$`](https://regex101.com/r/qujU6N/1) (a bit simplified/shortened) – bobble bubble Jun 25 '22 at 02:32
  • Works like a charm @bobble bubble. Thank You for the lightning fast response. Do you have any book that I can take a look or any suggestion from where I should learn this. – Avatar Girase Jun 25 '22 at 02:38
  • 1
    yw:) actually I learned the most while working a lot with regexes and here by answering and studying but if you like books: [Jeffrey Fridl - Mastering Regular Expressions](https://www.amazon.de/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124) - websites: [RexEgg](https://www.rexegg.com/), [Regular-Expressions.info](https://www.regular-expressions.info/) and for testing [Regex101](https://regex101.com/), [Regexstorm](http://regexstorm.net/tester) (.NET), [RegexPlanet](https://www.regexplanet.com/advanced/java/index.html) (Java) – bobble bubble Jun 25 '22 at 02:46
  • could you please help me on this https://stackoverflow.com/questions/72957182/regex-to-stop-user-from-entering-first-three-repeated-digit-except-888 – Avatar Girase Jul 12 '22 at 19:06
1

Please check this.

([0-8])\1{2}

I think this can help you. This is simpler and faster. If you have other questions, please. Good luck.

enter image description here

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 2
    Read the question carefully. The OP wants to match 3 digits **except** "999". Your matches 3 digits **including** "999". Also presenting your answer as a screenshot is unhelpful for programmers who have impaired vision. – Stephen C Jun 25 '22 at 02:21
  • ok. Please use ([0-8])\1{2} –  Jun 25 '22 at 02:34