private static StringPointer match(MatchState ms, StringPointer ss, StringPointer pp) {
        StringPointer s = ss.getClone();
        StringPointer p = pp.getClone();
        boolean isContinue = true;
        boolean isDefault = false;
        while (isContinue) {
            isContinue = false;
            isDefault = false;
            switch (p.getChar()) {
            case '(': { // start capture
                StringPointer p1 = p.getClone();
                if (p.getChar(1) == ')') { // position capture?
                    p1.postIncrString(2);
                    return startCapture(ms, s, p1, CAP_POSITION);
                } else {
                    p1.postIncrString(1);
                    return startCapture(ms, s, p1, CAP_UNFINISHED);
                }
            }
            case ')': { // end capture
                StringPointer p1 = p.getClone();
                p1.postIncrString(1);
                return endCapture(ms, s, p1);
            }
            case L_ESC: {
                switch (p.getChar(1)) {
                case 'b': { // balanced string?
                    StringPointer p1 = p.getClone();
                    p1.postIncrString(2);
                    s = matchBalance(ms, s, p1);
                    if (s == null) {
                        return null;
                    }
                    p.postIncrString(4);
                    isContinue = true;
                    continue; // else return match(ms, s, p+4);
                }
                case 'f': { // frontier?
                    p.postIncrString (2);
                    BaseLib.luaAssert(p.getChar() == '[' , "missing '[' after '%%f' in pattern");

                    StringPointer ep = classEnd(p);  // points to what is next
                    char previous = (s.getIndex() == ms.src_init.getIndex()) ? '\0' : s.getChar(-1);

                    StringPointer ep1 = ep.getClone();
                    ep1.postIncrString(-1);
                    if (matchBracketClass(previous, p, ep1) || !matchBracketClass(s.getChar(), p, ep1)) {
                        return null;
                    }
                    p = ep;
                    isContinue = true;
                    continue; // else return match(ms, s, ep);
                }
                default: {
                    if (Character.isDigit(p.getChar(1))) { // capture results (%0-%9)?
                        s = matchCapture(ms, s, p.getChar(1));
                        if (s == null) {
                            return null;
                        }
                        p.postIncrString(2);
                        isContinue = true;
                        continue; // else return match(ms, s, p+2)
                    }
                    isDefault = true; // case default
                }
                }
                break;
            }
            case '\0': {  // end of pattern
                return s;  // match succeeded
            }
            case '$': {
                if (p.getChar(1) == '\0') { // is the `$' the last char in pattern?
                    return (s.getIndex() == ms.endIndex) ? s : null;  // check end of string
                }
            }
            default: { // it is a pattern item
                isDefault = true;
            }
            }

            if (isDefault) { // it is a pattern item
                isDefault = false;
                StringPointer ep = classEnd(p);  // points to what is next
                boolean m = (s.getIndex () < ms.endIndex && singleMatch(s.getChar(), p, ep));
                switch (ep.getChar()) {
                case '?':  { // optional
                    StringPointer res;
                    StringPointer s1 = s.getClone();
                    s1.postIncrString ( 1 );
                    StringPointer ep1 = ep.getClone();
                    ep1.postIncrString ( 1 );

                    if (m && ((res = match(ms, s1, ep1)) != null)) {
                        return res;
                    }
                    p = ep;
                    p.postIncrString(1);
                    isContinue = true;
                    continue; // else return match(ms, s, ep+1);
                }
                case '*': { // 0 or more repetitions
                    return maxExpand(ms, s, p, ep);
                }
                case '+': { // 1 or more repetitions
                    StringPointer s1 = s.getClone();
                    s1.postIncrString(1);
                    return (m ? maxExpand(ms, s1, p, ep) : null);
                }
                case '-': { // 0 or more repetitions (minimum)
                    return minExpand(ms, s, p, ep);
                }
                default: {
                    if (!m) {
                        return null;
                    }
                    s.postIncrString(1);

                    p = ep;
                    isContinue = true;
                    continue; // else return match(ms, s+1, ep);
                }
                }
            }
        }
        return null;
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java