^ # N = tail (?*(x(x*)x+)) # \1 = N - A; \2 = N - B - 1 (?! # Assert that both A and B are perfect powers (\1|x\2) # tail = either A or B (?! (x+) ( (\4(x+)) (?=(\6*)\4*$) \6*(?=\7$\8) )+ x$ | x$ ) ) .*(?=\1$) # tail = N - A # Assert that tail==(N-B)^M where M>=2 \2 ( (\2(x+)) (?=(\10*)\2*$) \10*(?=\11$\12) )+ x$