private static StringPointer maxExpand(MatchState ms, StringPointer s, StringPointer p, StringPointer ep) {
        int i = 0;  // counts maximum expand for item
        while (s.getIndex () + i < ms.endIndex && singleMatch(s.getChar(i), p, ep)) {
            i ++;
        }
        // keeps trying to match with the maximum repetitions
        while (i >= 0) {
            StringPointer sp1 = s.getClone();
            sp1.postIncrString(i);
            StringPointer sp2 = ep.getClone();
            sp2.postIncrString(1);
            StringPointer res = match(ms, sp1, sp2);
            if (res != null) {
                return res;
            }
            i --;  // else didn't match; reduce 1 repetition to try again
        }
        return null;
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java