m/^(?:(\d+)|::)*\1$/x #which means the same as: m/^ (\d+)* #capture digits from the beginning of string \1 #than match the same digit as $1 in the end #and the backtracking (in reverse order) ends happily #where $1 is the digit before the last one $/;