{"record":{"id":"c1d1d73d2a44b309","repo":"stanfordnlp/CoreNLP","slug":"bad-pattern-sourcepattern","errorCode":null,"errorMessage":"Bad pattern: <sourcePattern>","messagePattern":"Bad pattern: <sourcePattern>","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/GrammaticalRelation.java","lineNumber":296,"sourceCode":"                             String sourcePattern,\n                             TregexPatternCompiler tregexCompiler,\n                             String[] targetPatterns,\n                             String specificString) {\n    this.language = language;\n    this.shortName = shortName;\n    this.longName = longName;\n    this.parent = parent;\n    this.specific = specificString; // this can be null!\n\n    if (parent != null) {\n      parent.addChild(this);\n    }\n\n    if (sourcePattern != null) {\n      try {\n        this.sourcePattern = Pattern.compile(sourcePattern);\n      } catch (java.util.regex.PatternSyntaxException e) {\n        throw new RuntimeException(\"Bad pattern: \" + sourcePattern);\n      }\n    } else {\n      this.sourcePattern = null;\n    }\n\n    for (String pattern : targetPatterns) {\n      try {\n        TregexPattern p = tregexCompiler.compile(pattern);\n        this.targetPatterns.add(p);\n      } catch (edu.stanford.nlp.trees.tregex.TregexParseException pe) {\n        throw new RuntimeException(\"Bad pattern: \" + pattern, pe);\n      }\n    }\n\n    GrammaticalRelation previous;\n    synchronized (stringsToRelations) {\n      Map<String, GrammaticalRelation> sToR = stringsToRelations.get(language);\n      if (sToR == null) {","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/GrammaticalRelation.java#L278-L314","documentation":"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.","triggerScenarios":"Constructing a new GrammaticalRelation(...) passing a sourcePattern argument containing invalid regex syntax (e.g. unbalanced parentheses, dangling quantifier like 'foo**' or stray '[class').","commonSituations":"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.","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"],"exampleFix":"// before\nnew GrammaticalRelation(lang, \"nsubj\", \"nominal subject\", \"**bad[\", null, ...);\n// after\nnew GrammaticalRelation(lang, \"nsubj\", \"nominal subject\", \"^(?!.*\\\\b(expletive)\\\\b).*$\", null, ...);","handlingStrategy":"validation","validationCode":"try {\n    java.util.regex.Pattern.compile(sourcePattern);\n} catch (java.util.regex.PatternSyntaxException e) {\n    throw new IllegalArgumentException(\"Invalid sourcePattern: \" + sourcePattern, e);\n}","typeGuard":null,"tryCatchPattern":"try {\n    GrammaticalRelation r = new GrammaticalRelation(lang, name, longName, sourcePattern, ...);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Bad pattern:\")) {\n        // fix regex or fall back to null sourcePattern\n    } else throw e;\n}","preventionTips":["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"],"tags":["java","regex","pattern"],"backgroundTag":"invalid-regex-pattern","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"}