private static String addString(MatchState ms, Object repl, StringPointer s, StringPointer e) {
        String replTemp = BaseLib.tostring(repl, ms.callFrame.thread.state);
        StringPointer replStr = new StringPointer (replTemp);
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < replTemp.length(); i++) {
            if (replStr.getChar ( i ) != L_ESC) {
                buf.append(replStr.getChar(i));
            } else {
                i ++;  // skip ESC
                if (!Character.isDigit(replStr.getChar(i))) {
                    buf.append(replStr.getChar(i));
                } else if (replStr.getChar(i) == '0') {
                    String str = s.getString ();
                    int len = s.length() - e.length();
                    if (len > str.length() ) {
                        len = str.length();
                    }
                    buf.append(str.substring(0, len));
                } else {
                    int captureIndex = replStr.getChar(i) - '1';
                    Object[] captures = ms.getCaptures();
                    if (captures == null || captureIndex > ms.level) {
                        throw new IllegalStateException("invalid capture index");
                    }
                    Object o = captures[captureIndex];
                    if(o instanceof Double) {
                        Double doubleValue = ((Double)o);
                        if( doubleValue.doubleValue() - doubleValue.intValue() == 0 ) {
                            buf.append(String.valueOf(((Double)o).intValue()));
                        } else {
                            buf.append(String.valueOf(((Double)o).doubleValue()));
                        }
                    } else {
                        buf.append(o);
                    }
                }
            }
        }
        return buf.toString();
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java