int (List<RegExpMatch> matches, int offset) { int index = -1; for (RegExpMatch match : matches) { final int count = match.count(); for (int i = 0; i < count; i++) { final int start = match.start(i); if (start <= offset && match.end(i) >= offset) { index = i; // don't break here, because there may be a better matching group inside the current group } else if (start > offset) { break; } } } return index; }