final String pattern) throws InvalidPatternException {
int firstValidCharClassIndex = indexOfStartBracket + 1;
int firstValidEndBracketIndex = indexOfStartBracket + 2;
if(indexOfStartBracket + 1 >= pattern.length()) {
throw new NoClosingBracketException(indexOfStartBracket, "[", "]",
pattern);
}
if(pattern.charAt(firstValidCharClassIndex) == '!') {
firstValidCharClassIndex++;
firstValidEndBracketIndex++;
}
final Matcher charClassStartMatcher = characterClassStartPattern
.matcher(pattern);
int groupEnd = -1;
while(groupEnd == -1) {
final int possibleGroupEnd = pattern.indexOf(']',
firstValidEndBracketIndex);
if(possibleGroupEnd == -1) {
throw new NoClosingBracketException(indexOfStartBracket, "[",
"]", pattern);
}
final boolean foundCharClass = charClassStartMatcher
.find(firstValidCharClassIndex);
if(foundCharClass
&& charClassStartMatcher.start() < possibleGroupEnd) {
final String classStart = charClassStartMatcher.group(0);
final String classEnd = classStart.charAt(1) + "]";
final int classStartIndex = charClassStartMatcher.start();
final int classEndIndex = pattern.indexOf(classEnd,
classStartIndex + 2);
if(classEndIndex == -1) {
throw new NoClosingBracketException(classStartIndex,
classStart, classEnd, pattern);
}
firstValidCharClassIndex = classEndIndex + 2;
firstValidEndBracketIndex = firstValidCharClassIndex;
}