Adam Hunyadi
Adam Hunyadi

Reputation: 1952

Regex multiple exclusions

I would like to make a regex that matches groups like abbc, where each letter is a different character.

Example:

bank (not matched, because the second and the third character is not the same)

rook (matched)

book (matched)

poop (not matched, because the first and the last character is the same)

So far I have been trying something like this:

(.)(.(?!\1))\2(.(?!\1)(?!\2))

This however matches poop too. How do I correct this?

Upvotes: 0

Views: 77

Answers (1)

Sebastian Proske
Sebastian Proske

Reputation: 8413

Your positioning of the lookaheads is a bit off, you can do it like

(.)(?!\1)(.)\2(?!\1|\2)(.)

See https://regex101.com/r/heBJar/1

You might need to apply anchors or word boundaries as needed. You should also consider using [a-z] or [[:alpha:]] or similar instead of the ..

Upvotes: 1

Related Questions