// e.g. s = "hello $1" and m = Matches("world") static S expandDollarRefsToMatches(S s, Matches m) { ret expandDollarRefsToMatches(s, m, false); } static S expandDollarRefsToMatches(S s, Matches m, bool alwaysQuote) { L tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { S t = tok.get(i); if (!t.startsWith("$")) continue; t = dropFirst(t); int j = parseIntAtEnd(t); if (j < 1) continue; S x = get(m.m, j-1); if (x == null) continue; if (alwaysQuote || startsWith(t, "q")) x = quote(x); tok.set(i, x); } ret join(tok); }