stanfordnlp/CoreNLP · error · IndexOutOfBoundsException

Invalid region start=

Error message

Invalid region start=

What it means

SequenceMatcher.find(int start) validates that the starting index is within the current region: 0 <= start <= elements.size(). An out-of-bounds start throws IndexOutOfBoundsException 'Invalid region start=..., need to be between 0 and N'. This mirrors java.util.regex.Matcher.find(int) semantics.

Solutions

  1. Clamp/validate start before calling: if (start < 0 || start > matcher.regionEnd()) skip/stop
  2. When continuing after a match, guard: if (m.end() < elements.size()) find(m.end()); else stop
  3. Reset the matcher and recompute indices whenever the underlying sequence changes

Example fix

// before
matcher.find(matcher.end() + 1); // can exceed size
// after
int next = matcher.end() + 1;
if (next <= elements.size()) {
  matcher.find(next);
} else {
  // done scanning
}
Defensive patterns

Strategy: validation

Validate before calling

if (start >= 0 && start <= elements.size()) {
  matcher.find(start);
}

Type guard

boolean isValidStart(SequenceMatcher<?> m, int start) {
  return start >= 0 && start <= m.regionEnd();
}

Try / catch

try {
  found = matcher.find(start);
} catch (IndexOutOfBoundsException e) {
  if (e.getMessage().startsWith("Invalid region start")) {
    matcher.reset();
    found = matcher.find(); // restart from beginning
  }
}

Prevention

When it happens

Trigger: find(-1), find(elements.size()+1), or find(start) computed from stale indices after the sequence changed (region reset or a different, shorter list) — e.g. calling find on a new matcher with an index saved from a previous match on another sequence.

Common situations: Iterating with match.end()+1 after the last match: end() == size() leads to find(size()+1) misuse; reusing indices across documents/sequences of different lengths; off-by-one in manual scanning loops.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/b0b9e783becc26d5. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java:334

  public boolean isMatchWithResult() {
    return matchWithResult;
  }

  public void setMatchWithResult(boolean matchWithResult) {
    this.matchWithResult = matchWithResult;
  }

  /**
   * Reset the matcher and then searches for pattern at the specified start index.
   *
   * @param start - Index at which to start the search
   * @return true if a match is found (false otherwise)
   * @throws IndexOutOfBoundsException if start is {@literal <} 0 or larger then the size of the sequence
   * @see #find()
   */
  public boolean find(int start) {
    if (start < 0 || start > elements.size()) {
      throw new IndexOutOfBoundsException("Invalid region start=" + start + ", need to be between 0 and " + elements.size());
    }
    reset();
    return find(start, false);
  }

  protected boolean find(int start, boolean matchStart) {
    boolean done = false;
    while (!done) {
      boolean res = find0(start, matchStart);
      if (res) {
        boolean empty = this.group().isEmpty();
        if (!empty || includeEmptyMatches) return res;
        else {
          start = start + 1;
        }
      }
      done = !res;
    }

View on GitHub (pinned to 1b7edd19c4)