I am looking for a regex to match words formed with specific characters without repeating any character: Example, for a b c and d, how to specify a regex to match those strings:
bdca (match) adb (match) abcg (fail) aab (fail) I tried with ^[abcd]{1,4}$ but it accepts repeated characters (last example).
Please any help?
You can use this regex based on negative lookahead:
^(?:([abcd])(?!.*\1)){1,4}$
RegEx Demo
Breakup:
^ Line start
(?: Start non-capturing group
([abcd]) Match a or b or c or d and group it
(?!.*\1) Negative lookahead to fail the match if same grouped char is ahead
){1,4} 1 to 4 occurrences of non-capturing group
$ Line end
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