I think the problem is how you define source . On my system, this is:
String source = Pattern.quote("{% assign foo = values %}.{{ foo[0] }}.");
equivalent to this:
String source = "\\Q{% assign foo = values %}.{{ foo[0] }}.\\E";
(i.e. adds stray \Q and \E ), but the way you define the method, your Java implementation can treat it like an equivalent:
String source = "\\{% assign foo = values %\\}\\.\\{\\{ foo\\[0\\] \\}\\}\\.";
(i.e., inserting a large number of backslashes).
Your regex seems beautiful. This program:
public static void main(final String... args) { final Pattern p = Pattern.compile("(?-mix:((?-mix:(?-mix:\\{\\%).*?(?-mix:\\%\\})|(?-mix:\\{\\{).*?(?-mix:\\}\\}?))|(?-mix:\\{\\{|\\{\\%)))"); for(final String s : p.split("a{%b%}c{{d}}e{%f%}g{{h}}i{{j{%k")) System.out.println(s); }
prints
a c e g i j k
that is, it successfully processes {%b%} , {{d}} , {%f%} , {{h}} , {{ and {% as divided points with all the greed that you expect. But recording the record also works if I draw p all the way to
Pattern.compile("\\{%.*?%\\}|\\{\\{.*?\\}\\}?|\\{\\{|\\{%");
; -)
ruakh source share