String (@NotNull CharSequence string, @Nullable StringWithId matchingString, @NotNull MatchData matchData) { if (matchingString == null || !matchData.matched()) { return string.toString(); } StringBuilder result = new StringBuilder(); int charIndex = 0; int length = string.length(); while (charIndex < length) { char c = string.charAt(charIndex); if (c == '\\') { boolean hasGroupIndex = false; int groupIndex = 0; int digitIndex = charIndex + 1; while (digitIndex < length) { int digit = Character.digit(string.charAt(digitIndex), 10); if (digit == -1) { break; } hasGroupIndex = true; groupIndex = groupIndex * 10 + digit; digitIndex++; } if (hasGroupIndex && matchData.count() > groupIndex) { TextMateRange range = matchData.byteOffset(groupIndex); Strings.escapeToRegexp(new String(matchingString.bytes, range.start, range.getLength(), StandardCharsets.UTF_8), result); charIndex = digitIndex; continue; } } result.append(c); charIndex++; } return result.toString(); }