{"record":{"id":"b0b9e783becc26d5","repo":"stanfordnlp/CoreNLP","slug":"invalid-region-start","errorCode":null,"errorMessage":"Invalid region start=","messagePattern":"Invalid region start=","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java","lineNumber":334,"sourceCode":"  public boolean isMatchWithResult() {\n    return matchWithResult;\n  }\n\n  public void setMatchWithResult(boolean matchWithResult) {\n    this.matchWithResult = matchWithResult;\n  }\n\n  /**\n   * Reset the matcher and then searches for pattern at the specified start index.\n   *\n   * @param start - Index at which to start the search\n   * @return true if a match is found (false otherwise)\n   * @throws IndexOutOfBoundsException if start is {@literal <} 0 or larger then the size of the sequence\n   * @see #find()\n   */\n  public boolean find(int start) {\n    if (start < 0 || start > elements.size()) {\n      throw new IndexOutOfBoundsException(\"Invalid region start=\" + start + \", need to be between 0 and \" + elements.size());\n    }\n    reset();\n    return find(start, false);\n  }\n\n  protected boolean find(int start, boolean matchStart) {\n    boolean done = false;\n    while (!done) {\n      boolean res = find0(start, matchStart);\n      if (res) {\n        boolean empty = this.group().isEmpty();\n        if (!empty || includeEmptyMatches) return res;\n        else {\n          start = start + 1;\n        }\n      }\n      done = !res;\n    }","sourceCodeStart":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java#L316-L352","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Clamp/validate start before calling: if (start < 0 || start > matcher.regionEnd()) skip/stop","When continuing after a match, guard: if (m.end() < elements.size()) find(m.end()); else stop","Reset the matcher and recompute indices whenever the underlying sequence changes"],"exampleFix":"// before\nmatcher.find(matcher.end() + 1); // can exceed size\n// after\nint next = matcher.end() + 1;\nif (next <= elements.size()) {\n  matcher.find(next);\n} else {\n  // done scanning\n}","handlingStrategy":"validation","validationCode":"if (start >= 0 && start <= elements.size()) {\n  matcher.find(start);\n}","typeGuard":"boolean isValidStart(SequenceMatcher<?> m, int start) {\n  return start >= 0 && start <= m.regionEnd();\n}","tryCatchPattern":"try {\n  found = matcher.find(start);\n} catch (IndexOutOfBoundsException e) {\n  if (e.getMessage().startsWith(\"Invalid region start\")) {\n    matcher.reset();\n    found = matcher.find(); // restart from beginning\n  }\n}","preventionTips":["Clamp start to [0, elements.size()] before find(int)","Guard continuation loops with end() < size() checks","Recompute indices after resetting the matcher or changing the sequence","Never reuse match indices across different sequences"],"tags":["java","tokensregex","index-out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}