stanfordnlp/CoreNLP · error · java.lang.IllegalArgumentException
StringRegex binding error: Invalid variable name
Error message
StringRegex binding error: Invalid variable name
What it means
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.
Solutions
- Prefix the variable name with $ and use only letters, digits, and underscores: $VAR1
- Sanitize generated names before calling bindStringRegex
Example fix
// before
env.bindStringRegex("myvar", "foo|bar");
// after
env.bindStringRegex("$myvar", "foo|bar"); Defensive patterns
Strategy: validation
Validate before calling
private static final Pattern NAME = Pattern.compile("\\$[A-Za-z0-9_]+");
if (var == null || !NAME.matcher(var).matches()) throw new IllegalArgumentException("StringRegex var must match $[A-Za-z0-9_]+: " + var);
env.bindStringRegex(var, regex); Type guard
boolean isValidStringRegexVar(String v) { return v != null && v.matches("\\$[A-Za-z0-9_]+"); } Try / catch
try { env.bindStringRegex(var, regex); } catch (IllegalArgumentException e) { log.warn("Skipping invalid string-regex var: {}", var); } Prevention
- Always include the leading $ in variable names
- Restrict generated names to [A-Za-z0-9_]
- Unit-test variable binding with edge-case names
When it happens
Trigger: Calling env.bindStringRegex(var, regex) with a name like 'myvar' (no leading $), '$my-var', or an empty string.
Common situations: Programmatic binding of substitution variables with auto-generated names, or misunderstanding that the $ prefix is mandatory.
Understand the failure class
Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.
Related errors
- Unable get expression for variable
- 2 arguments expected, got
- Annotation field cannot be null
- : Entry has multiple types for : . Taking type to be
- Attribute already defined:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8bb8c1b384635e49.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/Env.java:289
public void setDefaultStringPatternFlags(int defaultStringPatternFlags) {
this.defaultStringPatternFlags = defaultStringPatternFlags;
}
public int getDefaultStringMatchFlags() {
return defaultStringMatchFlags;
}
public void setDefaultStringMatchFlags(int defaultStringMatchFlags) {
this.defaultStringMatchFlags = defaultStringMatchFlags;
}
private static final Pattern STRING_REGEX_VAR_NAME_PATTERN = Pattern.compile("\\$[A-Za-z0-9_]+");
public void bindStringRegex(String var, String regex)
{
// Enforce requirements on variable names ($alphanumeric_)
if (!STRING_REGEX_VAR_NAME_PATTERN.matcher(var).matches()) {
throw new IllegalArgumentException("StringRegex binding error: Invalid variable name " + var);
}
Pattern varPattern = Pattern.compile(Pattern.quote(var));
String replace = Matcher.quoteReplacement(regex);
stringRegexVariables.put(var, new Pair<>(varPattern, replace));
}
public String expandStringRegex(String regex) {
// Replace all variables in regex
String expanded = regex;
for (Map.Entry<String, Pair<Pattern, String>> stringPairEntry : stringRegexVariables.entrySet()) {
Pair<Pattern,String> p = stringPairEntry.getValue();
expanded = p.first().matcher(expanded).replaceAll(p.second());
}
return expanded;
}
public Pattern getStringPattern(String regex) {
String expanded = expandStringRegex(regex);View on GitHub (pinned to 1b7edd19c4)