stanfordnlp/CoreNLP · error · IllegalArgumentException
Invalid captureGroupId=
Error message
Invalid captureGroupId=
What it means
BackRefPatternExpr matches a previously captured group; its constructor requires captureGroupId to be a positive group index (>= 1). A non-positive value throws IllegalArgumentException 'Invalid captureGroupId='. Group ids are 1-based, mirroring regex back-reference semantics.
Solutions
- Ensure the referenced capture group exists and its id is >= 1 before creating the back reference
- Create groups first, capture their assigned ids, then build BackRefPatternExpr with that id
- Validate user-provided group indices: reject <= 0 early with a clear message
- Use \1-style back references only after at least one group in the pattern
Example fix
// before new BackRefPatternExpr(matcher, 0); // invalid group id // after new BackRefPatternExpr(matcher, 1); // first captured group
Defensive patterns
Strategy: validation
Validate before calling
if (captureGroupId <= 0) {
throw new IllegalArgumentException("captureGroupId must be >= 1, got: " + captureGroupId);
} Type guard
boolean isValidCaptureGroupId(int id) {
return id >= 1;
} Try / catch
try {
BackRefPatternExpr expr = new BackRefPatternExpr(matcher, groupId);
} catch (IllegalArgumentException e) {
log.error("Bad capture group id", e);
} Prevention
- Use 1-based group numbering consistently
- Only emit back references after the referenced group is defined
- Validate group ids when parsing user-supplied pattern specs
When it happens
Trigger: Constructing a BackRefPatternExpr (directly or via a parser/builder) with captureGroupId <= 0, e.g. passing 0 as a default or computing a group index that was never assigned.
Common situations: Programmatic pattern building where the back-reference is emitted before any group was defined, or off-by-one handling of 1-based capture group numbering.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Invalid nextBranchIndex=
- Error compiling
- Invalid minMatch=
- Cannot put a child trie with no keys
- Value cannot be null
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/9cadd6e8e5e8ad9e.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/SequencePattern.java:556
return new SequencePatternExpr(newPatterns);
}
public String toString() {
return StringUtils.join(patterns, " ");
}
}
// Expression that indicates a back reference
// Need to match a previously matched group somehow
public static class BackRefPatternExpr extends PatternExpr {
private static final long serialVersionUID = -4649629486266561619L;
private final NodesMatchChecker matcher; // How a match is determined
private final int captureGroupId; // Indicates the previously matched group this need to match
public BackRefPatternExpr(NodesMatchChecker matcher, int captureGroupId) {
if (captureGroupId <= 0) { throw new IllegalArgumentException("Invalid captureGroupId=" + captureGroupId); }
this.captureGroupId = captureGroupId;
this.matcher = matcher;
}
@Override
protected Frag build() {
State s = new BackRefState(matcher, captureGroupId);
return new Frag(s);
}
@Override
protected int assignGroupIds(int start) {
return start;
}
@Override
protected void updateBindings(VarGroupBindings bindings) {}
@OverrideView on GitHub (pinned to 1b7edd19c4)