{"record":{"id":"8bb8c1b384635e49","repo":"stanfordnlp/CoreNLP","slug":"stringregex-binding-error-invalid-variable-name","errorCode":null,"errorMessage":"StringRegex binding error: Invalid variable name ","messagePattern":"StringRegex binding error: Invalid variable name ","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/Env.java","lineNumber":289,"sourceCode":"\n  public void setDefaultStringPatternFlags(int defaultStringPatternFlags) {\n    this.defaultStringPatternFlags = defaultStringPatternFlags;\n  }\n\n  public int getDefaultStringMatchFlags() {\n    return defaultStringMatchFlags;\n  }\n\n  public void setDefaultStringMatchFlags(int defaultStringMatchFlags) {\n    this.defaultStringMatchFlags = defaultStringMatchFlags;\n  }\n\n  private static final Pattern STRING_REGEX_VAR_NAME_PATTERN = Pattern.compile(\"\\\\$[A-Za-z0-9_]+\");\n  public void bindStringRegex(String var, String regex)\n  {\n    // Enforce requirements on variable names ($alphanumeric_)\n    if (!STRING_REGEX_VAR_NAME_PATTERN.matcher(var).matches()) {\n      throw new IllegalArgumentException(\"StringRegex binding error: Invalid variable name \" + var);\n    }\n    Pattern varPattern = Pattern.compile(Pattern.quote(var));\n    String replace = Matcher.quoteReplacement(regex);\n    stringRegexVariables.put(var, new Pair<>(varPattern, replace));\n  }\n\n  public String expandStringRegex(String regex) {\n    // Replace all variables in regex\n    String expanded = regex;\n    for (Map.Entry<String, Pair<Pattern, String>> stringPairEntry : stringRegexVariables.entrySet()) {\n      Pair<Pattern,String> p = stringPairEntry.getValue();\n      expanded = p.first().matcher(expanded).replaceAll(p.second());\n    }\n    return expanded;\n  }\n\n  public Pattern getStringPattern(String regex) {\n    String expanded = expandStringRegex(regex);","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/Env.java#L271-L307","documentation":"Env.bindStringRegex enforces that string-regex substitution variable names match $[A-Za-z0-9_]+. Binding a name with other characters (spaces, hyphens, missing $, unicode) throws this IllegalArgumentException before any substitution happens.","triggerScenarios":"Calling env.bindStringRegex(var, regex) with a name like 'myvar' (no leading $), '$my-var', or an empty string.","commonSituations":"Programmatic binding of substitution variables with auto-generated names, or misunderstanding that the $ prefix is mandatory.","solutions":["Prefix the variable name with $ and use only letters, digits, and underscores: $VAR1","Sanitize generated names before calling bindStringRegex"],"exampleFix":"// before\nenv.bindStringRegex(\"myvar\", \"foo|bar\");\n// after\nenv.bindStringRegex(\"$myvar\", \"foo|bar\");","handlingStrategy":"validation","validationCode":"private static final Pattern NAME = Pattern.compile(\"\\\\$[A-Za-z0-9_]+\");\nif (var == null || !NAME.matcher(var).matches()) throw new IllegalArgumentException(\"StringRegex var must match $[A-Za-z0-9_]+: \" + var);\nenv.bindStringRegex(var, regex);","typeGuard":"boolean isValidStringRegexVar(String v) { return v != null && v.matches(\"\\\\$[A-Za-z0-9_]+\"); }","tryCatchPattern":"try { env.bindStringRegex(var, regex); } catch (IllegalArgumentException e) { log.warn(\"Skipping invalid string-regex var: {}\", var); }","preventionTips":["Always include the leading $ in variable names","Restrict generated names to [A-Za-z0-9_]","Unit-test variable binding with edge-case names"],"tags":["tokensregex","variables","naming"],"backgroundTag":"invalid-identifier-format","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"}