m{ ^ (?= \d\d\d\d $ ) # ensure it's only 4 digits long (?: # first digit is the duplicated one (\d) \1 (?!\1) \d (?!\1) \d | (\d) (?!\2) \d \2 (?!\2) \d | (\d) (?!\3) \d (?!\3) \d \3 # second digit is the duplicated one | (\d) (?!\4) (\d) \5 (?!\4|\5) \d | (\d) (?!\6) (\d) (?!\6|\7) \d \7 # third digit is the duplicated one | (\d) (\d) (?!\8|\9) (\d) \10 ) }x; #### m{ ^ (?= \d\d\d\d $ ) # ensure it's only 4 digits long (?: # first digit is the duplicated one (\d) (?: \1 (?!\1) \d (?!\1) \d | (?!\1) \d \1 (?!\1) \d | (?!\1) \d (?!\1) \d \1 ) # second digit is the duplicated one | (\d) (?!\2) (\d) (?: \3 (?!\2|\3) \d | (?!\2|\3) \d \3 ) # third digit is the duplicated one | (\d) (\d) (?!\4|\5) (\d) \6 ) }x; #### m{ ^ (?= \d* (\d) \d* \1 ) (?! \d* \1 \d* \1 \d* \1 ) }x;