0

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

  • it should contain three capture groups
  • all the capture groups should contain different characters
  • the second one should be matched exactly twice

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?

1
  • Regex might not be the correct tool for this. Commented Nov 16, 2017 at 11:12

1 Answer 1

1

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 ..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.