LevelPattern (@NotNull String text, LevelPattern @NotNull [] patterns) { int bestStart = Integer.MAX_VALUE; LevelPattern bestMatch = null; for (LevelPattern next : patterns) { Matcher nextMatcher = next.getPattern().matcher(text); if (nextMatcher.find()) { int nextStart = nextMatcher.start(); if (nextStart < bestStart) { bestMatch = next; bestStart = nextStart; } } } return bestMatch; }