{"record":{"id":"9cadd6e8e5e8ad9e","repo":"stanfordnlp/CoreNLP","slug":"invalid-capturegroupid","errorCode":null,"errorMessage":"Invalid captureGroupId=","messagePattern":"Invalid captureGroupId=","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequencePattern.java","lineNumber":556,"sourceCode":"      return new SequencePatternExpr(newPatterns);\n    }\n\n    public String toString() {\n      return StringUtils.join(patterns, \" \");\n    }\n  }\n\n  // Expression that indicates a back reference\n  // Need to match a previously matched group somehow\n  public static class BackRefPatternExpr extends PatternExpr {\n\n    private static final long serialVersionUID = -4649629486266561619L;\n\n    private final NodesMatchChecker matcher; // How a match is determined\n    private final int captureGroupId;  // Indicates the previously matched group this need to match\n\n    public BackRefPatternExpr(NodesMatchChecker matcher, int captureGroupId) {\n      if (captureGroupId <= 0) { throw new IllegalArgumentException(\"Invalid captureGroupId=\" + captureGroupId); }\n      this.captureGroupId = captureGroupId;\n      this.matcher = matcher;\n    }\n\n    @Override\n    protected Frag build() {\n      State s = new BackRefState(matcher, captureGroupId);\n      return new Frag(s);\n    }\n\n    @Override\n    protected int assignGroupIds(int start) {\n      return start;\n    }\n    @Override\n    protected void updateBindings(VarGroupBindings bindings) {}\n\n    @Override","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequencePattern.java#L538-L574","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nnew BackRefPatternExpr(matcher, 0); // invalid group id\n// after\nnew BackRefPatternExpr(matcher, 1); // first captured group","handlingStrategy":"validation","validationCode":"if (captureGroupId <= 0) {\n  throw new IllegalArgumentException(\"captureGroupId must be >= 1, got: \" + captureGroupId);\n}","typeGuard":"boolean isValidCaptureGroupId(int id) {\n  return id >= 1;\n}","tryCatchPattern":"try {\n  BackRefPatternExpr expr = new BackRefPatternExpr(matcher, groupId);\n} catch (IllegalArgumentException e) {\n  log.error(\"Bad capture group id\", e);\n}","preventionTips":["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"],"tags":["java","illegal-argument","tokensregex","regex"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}