{"record":{"id":"f9c3318e2f7a942f","repo":"stanfordnlp/CoreNLP","slug":"bad-pattern-pattern","errorCode":null,"errorMessage":"Bad pattern: <pattern>","messagePattern":"Bad pattern: <pattern>","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/GrammaticalRelation.java","lineNumber":307,"sourceCode":"      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) {\n        sToR = Generics.newHashMap();\n        stringsToRelations.put(language, sToR);\n      }\n      previous = sToR.put(toString(), this);\n    }\n    if (previous != null) {\n      if ( ! previous.isFromString() && ! isFromString()) {\n        throw new IllegalArgumentException(\"There is already a relation named \" + this + '!');\n      } else {\n        /* We get here if we previously just built a fake relation from a string\n         * we previously read in from a file.","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/GrammaticalRelation.java#L289-L325","documentation":"During GrammaticalRelation construction, each target pattern string is compiled with the Tregex compiler. If a pattern is not valid Tregex syntax, TregexParseException is caught and rethrown as RuntimeException('Bad pattern: ' + pattern, pe). This means one of the relation's target tregex expressions cannot be parsed.","triggerScenarios":"Constructing a GrammaticalRelation whose targetPatterns list contains a string that fails Tregex parsing (bad node descriptions, unbalanced brackets, unsupported operators, missing quotes around regex labels).","commonSituations":"Writing custom relation definitions with hand-written tregex; version changes where tregex grammar became stricter; typos such as unbalanced '>' or '[ ]' in patterns like 'NP < NN'.","solutions":["Fix the tregex pattern syntax reported in the message; verify against the Tregex grammar for your library version","Quote regex label values inside patterns (e.g. /^NN/ usage rules) and balance all brackets/angle brackets","Compile the pattern standalone with a TregexPatternCompiler to get the full TregexParseException detail (also in the cause chain)","Check release notes for tregex syntax changes if the relation worked on an older CoreNLP version"],"exampleFix":"// before\ntargetPatterns = new String[]{ \"VP < (NP $-- ADJP\" }; // unbalanced paren\n// after\ntargetPatterns = new String[]{ \"VP < (NP $-- ADJP)\" };","handlingStrategy":"validation","validationCode":"TregexPatternCompiler compiler = new TregexPatternCompiler(headFinder);\nfor (String p : targetPatterns) {\n    try { compiler.compile(p); } catch (TregexParseException e) {\n        throw new IllegalArgumentException(\"Invalid tregex: \" + p, e);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    GrammaticalRelation r = new GrammaticalRelation(lang, name, longName, src, Arrays.asList(targetPatterns), ...);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Bad pattern:\")) {\n        TregexParseException cause = (TregexParseException) e.getCause(); // inspect exact syntax error\n    } else throw e;\n}","preventionTips":["Pre-compile all tregex patterns with TregexPatternCompiler in unit tests","Keep bracket/paren balance by formatting patterns across multiple lines","Quote regex labels per tregex rules","Re-run pattern tests when upgrading CoreNLP versions"],"tags":["java","tregex","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"}