Java with an infinite lookbehind length and nested links ( see also at ideone.com ):
import java.util.regex.*; class Factorial { static String assertPrefix(String pattern) { return "(?<=(?=^pattern).*)".replace("pattern", pattern); } public static void main(String[] args) { final Pattern FACTORIAL = Pattern.compile( "(?x) (?: inc stepUp)+" .replace("inc", "(?=(^|\\1 .))") // 1 .replace("stepUp", "(?: ^. | (?<=(^.*)) (?=(.*)) (?: notThereYet \\2)+ exactlyThere )") // 2 3 .replace("notThereYet", "(?: (?=((?=\\3) . | \\4 .)) (?=\\1(.*)) (?=\\4\\5) )") // 4 5 .replace("exactlyThere", "measure4 measure1") .replace("measure4", assertPrefix("\\4(.*)")) .replace("measure1", assertPrefix("\\1\\6")) ); for (int n = 0; n < 1000; n++) { Matcher m = FACTORIAL.matcher(new String(new char[n])); if (m.matches()) { System.out.printf("%3s = %s!%n", n, m.group(1).length() + 1); } } } }
source share