List<Substring> (@NotNull Pattern pattern, int maxSplits) { final List<Substring> result = new ArrayList<>(); final Matcher m = pattern.matcher(myString); int start = myStartOffset; int end = myEndOffset; int splitCount = 0; if (m.find(start)) { do { splitCount++; end = m.start(); result.add(createAnotherSubstring(start, Math.min(end, myEndOffset))); start = m.end(); } while (end < myEndOffset && m.find() && splitCount < maxSplits); if (start <= myEndOffset) { result.add(createAnotherSubstring(start, myEndOffset)); } } else { result.add(createAnotherSubstring(start, end)); } return result; }