Regex to match all permutations of {1,2,3,4} without repetition

You can use this (see on rubular.com): ^(?=[1-4]{4}$)(?!.*(.).*\1).*$ The first assertion ensures that it’s ^[1-4]{4}$, the second assertion is a negative lookahead that ensures that you can’t match .*(.).*\1, i.e. a repeated character. The first assertion is “cheaper”, so you want to do that first. References regular-expressions.info/Lookarounds and Backreferences Related questions How does the regular … Read more

Regular Expressions: Is there an AND operator?

You need to use lookahead as some of the other responders have said, but the lookahead has to account for other characters between its target word and the current match position. For example: (?=.*word1)(?=.*word2)(?=.*word3) The .* in the first lookahead lets it match however many characters it needs to before it gets to “word1”. Then … Read more