I am trying to match a specific set of characters but 1 and only 1 of each.
For example if the set is [abcd]
, I want to match string containing these exact characters in any order.
abcd - true
bcad - true
abc - false (need all characters)
abbd - false
abcdd - false
From what I understand so far there is no easy way to achieve this with RegEx but no answer was conclusive enough.
I would think of capturing and using a lookahead to check if the same character is not ahead.
\b(?:([abcd])(?!\w*?\1)){4}\b
(?:
opens a non capture group for repetition\b
matches a word boundary
([abcd])
captures one of [abcd]
(?!\w*?\1)
checks if the captured character is not ahead with any amount of \w
in between{4}\b
4 times until another word boundarySee demo at regex101 (works only, if a lookahead is available in your regex flavor)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With