There is an “elegant” way to do this with a single regex:
^(?:2()|3()|4()|5()){4}\1\2\3\4$
will match the numbers 2, 3, 4 and 5 in any order. All four are required.
Explanation:
(?:2()|3()|4()|5()) corresponds to one of the numbers 2, 3, 4 or 5. The trick now is that the sliding parentheses correspond to the empty line after matching the number (which is always performed).
{4} requires this to be done four times.
\1\2\3\4 , then it is required that all four backlinks participate in the match - what do they do if and only if each number happened once. Since \1\2\3\4 matches an empty string, it will always match as long as the previous condition is true.
For five digits you will need
^(?:2()|3()|4()|5()|6()){5}\1\2\3\4\5$
etc...
This will work with almost any regular expression except JavaScript.
source share