stanfordnlp/CoreNLP · error · RuntimeException
Bad pattern
Error message
Bad pattern: <sourcePattern>
What it means
The GrammaticalRelation constructor compiles the optional sourcePattern string into a java.util.regex.Pattern. If the string is not a syntactically valid regular expression, Pattern.compile throws PatternSyntaxException, which is wrapped and rethrown as this RuntimeException. It indicates a malformed regex in a relation definition.
Solutions
- Fix the regex syntax in the sourcePattern string (validate it with Pattern.compile in isolation or a regex tester)
- Escape metacharacters that were meant literally (e.g. use \. for a dot)
- If no source pattern is needed, pass null so the field is left null instead of compiling a bad string
- Test the pattern string against the exact Java regex dialect before adding the relation
Example fix
// before new GrammaticalRelation(lang, "nsubj", "nominal subject", "**bad[", null, ...); // after new GrammaticalRelation(lang, "nsubj", "nominal subject", "^(?!.*\\b(expletive)\\b).*$", null, ...);
Defensive patterns
Strategy: validation
Validate before calling
try {
java.util.regex.Pattern.compile(sourcePattern);
} catch (java.util.regex.PatternSyntaxException e) {
throw new IllegalArgumentException("Invalid sourcePattern: " + sourcePattern, e);
} Try / catch
try {
GrammaticalRelation r = new GrammaticalRelation(lang, name, longName, sourcePattern, ...);
} catch (RuntimeException e) {
if (e.getMessage().startsWith("Bad pattern:")) {
// fix regex or fall back to null sourcePattern
} else throw e;
} Prevention
- Test regex strings with Pattern.compile before wiring them into relations
- Escape regex metacharacters intended literally
- Prefer null sourcePattern when no source constraint is needed
- Use a regex linter/tester tuned to Java syntax
When it happens
Trigger: Constructing a new GrammaticalRelation(...) passing a sourcePattern argument containing invalid regex syntax (e.g. unbalanced parentheses, dangling quantifier like 'foo**' or stray '[class').
Common situations: Defining custom grammatical relations in code or config with a mistyped regex; copying patterns between regex dialects where escaping differs; recent edits introducing a typo in the pattern literal.
Related errors
- Bad pattern
- Error compiling
- Invalid captureGroupId=
- Node cannot be both negated and optional.
- String match result must be referred to by group id
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/c1d1d73d2a44b309.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/GrammaticalRelation.java:296
String sourcePattern,
TregexPatternCompiler tregexCompiler,
String[] targetPatterns,
String specificString) {
this.language = language;
this.shortName = shortName;
this.longName = longName;
this.parent = parent;
this.specific = specificString; // this can be null!
if (parent != null) {
parent.addChild(this);
}
if (sourcePattern != null) {
try {
this.sourcePattern = Pattern.compile(sourcePattern);
} catch (java.util.regex.PatternSyntaxException e) {
throw new RuntimeException("Bad pattern: " + sourcePattern);
}
} else {
this.sourcePattern = null;
}
for (String pattern : targetPatterns) {
try {
TregexPattern p = tregexCompiler.compile(pattern);
this.targetPatterns.add(p);
} catch (edu.stanford.nlp.trees.tregex.TregexParseException pe) {
throw new RuntimeException("Bad pattern: " + pattern, pe);
}
}
GrammaticalRelation previous;
synchronized (stringsToRelations) {
Map<String, GrammaticalRelation> sToR = stringsToRelations.get(language);
if (sToR == null) {View on GitHub (pinned to 1b7edd19c4)