private static Object pushOnecapture(MatchState ms, int i, StringPointer s, StringPointer e ) {
        if (i >= ms.level) {
            if ( i == 0 ) { // ms->level == 0, too
                String res = s.string.substring(s.index, e.index);
                ms.callFrame.push(res);
                return res;
            } else {
                throw new IllegalStateException("invalid capture index");
            }
        } else {
            int l = ms.capture[i].len;
            if (l == CAP_UNFINISHED) {
                throw new IllegalStateException("unfinished capture");
            } else if (l == CAP_POSITION) {
                Double res = new Double(ms.src_init.length() - ms.capture[i].init.length() + 1);
                ms.callFrame.push(res);
                return res;
            } else {
                int index = ms.capture[i].init.index;
                String res = ms.capture[i].init.string.substring(index, index+l);
                ms.callFrame.push(res);
                return res;
            }
        }
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java