List<TextRange> (Matcher matcher, final int length) { final List<TextRange> list = new SmartList<>(); int start = 0; while (start < length && matcher.find(start)) { final int groupCount = matcher.groupCount(); if (groupCount != 0) { for (int i = 1; i <= groupCount; i++) { start = matcher.start(i); if (start == -1) continue; list.add(new TextRange(start, matcher.end(i))); } if (start >= matcher.end()) break; } start = matcher.end(); } return list; }