{"record":{"id":"7ac3f42086a83312","repo":"stanfordnlp/CoreNLP","slug":"string-match-result-must-be-referred-to-by-group-i","errorCode":null,"errorMessage":"String match result must be referred to by group id","messagePattern":"String match result must be referred to by group id","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java","lineNumber":625,"sourceCode":"      if (DIGITS_PATTERN.matcher(group).matches()) {\n        Integer n = Integer.valueOf(group);\n        return new RegexMatchVarExpression(n);\n      } else {\n        return new RegexMatchVarExpression(group);\n      }\n    }\n    public Value evaluate(Env env, Object... args) {\n      if (args != null && args.length > 0) {\n        if (args[0] instanceof SequenceMatchResult) {\n          SequenceMatchResult mr = (SequenceMatchResult) args[0];\n          Object v = get();\n          if (v instanceof String) {\n            // TODO: depending if TYPE_STRING, use string version...\n            return new PrimitiveValue<>(TYPE_TOKENS, mr.groupNodes((String) v));\n          } else if (v instanceof Integer) {\n            return new PrimitiveValue<>(TYPE_TOKENS, mr.groupNodes((Integer) v));\n          } else {\n            throw new UnsupportedOperationException(\"String match result must be referred to by group id\");\n          }\n        } else if (args[0] instanceof MatchResult) {\n          MatchResult mr = (MatchResult) args[0];\n          Object v = get();\n          if (v instanceof Integer) {\n            String str = mr.group((Integer) get());\n            return new PrimitiveValue<>(TYPE_STRING, str);\n          } else {\n            throw new UnsupportedOperationException(\"String match result must be referred to by group id\");\n          }\n        }\n      }\n      return null;\n    }\n    public Expression assign(Expression expr) {\n      return new VarAssignmentExpression(value.toString(), expr, false);\n    }\n  }","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java#L607-L643","documentation":"In Expressions, a Tokens/GroupValue expression evaluated against a String match result must refer to capture groups by Integer group id. When the underlying value is a String (a group name) and the argument is a String-typed MatchResult wrapper that only supports numeric groups, groupNodes((String) v) is invalid for that result type and UnsupportedOperationException('String match result must be referred to by group id') is thrown.","triggerScenarios":"Evaluating an expression whose value is a named group reference (a String) with args[0] being a String match result (e.g. a regex match on a plain String rather than a token list), so the named-group lookup path is unavailable.","commonSituations":"TokensRegex expressions ported from token-level patterns to plain-string regex matching, where named groups used in TokensRegex are not supported by the String match result implementation.","solutions":["Change the expression to reference the group by integer index instead of by name.","Match against the token-sequence (CoreMap) matcher, which supports named group lookup, rather than a plain String match result.","Guard evaluation: only resolve String group names when the MatchResult supports named groups.","If using CoreNLP's Expression API, use TYPE_STRING/TOKENS group access consistent with the match result type you actually have."],"exampleFix":"// before\nExpressions.createTypedExpression(\"TOKENS\", \"$GROUPNAME\").evaluate(env, stringMatchResult);\n// after\nExpressions.createTypedExpression(\"TOKENS\", Integer.valueOf(1)).evaluate(env, stringMatchResult); // refer by group id","handlingStrategy":"type-guard","validationCode":"// Ensure group refs are integer ids when matching against String results\nif (groupRef instanceof String && matchResult instanceof java.util.regex.MatchResult) {\n  throw new IllegalArgumentException(\"Use integer group ids with String match results\");\n}","typeGuard":"boolean groupRefIsValid(Expression e, Object matchResult) {\n  Object v = ((Expressions.ValueExpression) e).get();\n  return !(matchResult instanceof java.util.regex.MatchResult) || v instanceof Integer;\n}","tryCatchPattern":"try {\n  return expr.evaluate(env, matchResult);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"referred to by group id\")) {\n    throw new IllegalArgumentException(\"Convert named group refs to indices for String match results\", e);\n  }\n  throw e;\n}","preventionTips":["Use integer group ids whenever evaluating against plain regex MatchResults.","Reserve named groups for the token/CoreMap matcher path.","Add a test for each group-reference expression against its intended match result type."],"tags":["expressions","regex","type-mismatch","java"],"backgroundTag":"type-mismatch","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"}