Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match string without repeated characters

Tags:

regex

I'm looking for a simple regular expression to match the string where no repeated characters. Example:

  • JHMCU26809C211501 - good
  • JHMGD18508S219366 - good
  • JHMCU268091111111 - bad
  • 12345678901234567 - good
  • ASD1111111ASD2313 - bad
  • ASDIIIIIIIASDASD2 - bad
  • IIIIIIIADS1EE2345 - bad
like image 615
Andrey M Avatar asked May 11 '26 14:05

Andrey M


2 Answers

You may do negation through negative lookahead.

^(?!.*(\w)\1{3,}).+$

DEMO

like image 147
Avinash Raj Avatar answered May 13 '26 04:05

Avinash Raj


If its a duplicate 3 or more times in a row, this is the fastest
way to do it. (no phony demo provided)

^(?:(.)(?!\1{2}))+$

 ^ 
 (?:
      ( . )                         # (1)
      (?! \1{2} )
 )+
 $

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!