{"record":{"id":"59b4af9e7d3583cd","repo":"languagetool-org/languagetool","slug":"regexp-rules-currently-cannot-be-used-together-w","errorCode":null,"errorMessage":"<regexp> rules currently cannot be used together with <antipattern>. Rule id: ","messagePattern":"<regexp> rules currently cannot be used together with <antipattern>\\. Rule id: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleHandler.java","lineNumber":794,"sourceCode":"        rule.addToneTags(ruleToneTags);\n        rule.addToneTags(ruleGroupToneTags);\n        rule.setSourceFile(sourceFile);\n        rule.setMinPrevMatches(minPrevMatches);\n        rule.setDistanceTokens(distanceTokens);\n        rule.setXmlLineNumber(xmlLineNumber);\n      } else if (regex.length() > 0) {\n        // UNICODE_CHARACTER_CLASS (equivalent to the inline (?U) flag) makes \\b, \\w, \\d and \\s\n        // Unicode-aware. Without it the behavior of word boundaries next to non-ASCII letters\n        // depends on the JDK version (it changed between JDK 17 and JDK 21), which breaks many\n        // <regexp> rules. Setting it here avoids adding (?U) by hand to every <regexp> pattern.\n        int flags = Pattern.UNICODE_CHARACTER_CLASS | (regexCaseSensitive ? 0 : Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);\n        String regexStr = regex.toString();\n        if (regexMode == RegexpMode.SMART) {\n          // Note: it's not that easy to add \\b because the regex might look like '(foo)' or '\\d' so we cannot just look at the last character\n          regexStr = replaceSpacesInRegex(regexStr);\n        }\n        if (ruleAntiPatterns.size() > 0 || rulegroupAntiPatterns.size() > 0) {\n          throw new RuntimeException(\"<regexp> rules currently cannot be used together with <antipattern>. Rule id: \" + id + \"[\" + subId + \"]\");\n        }\n        rule = new RegexPatternRule(id, name, message.toString(), shortMessage, suggestionsOutMsg.toString(), language, Pattern.compile(regexStr, flags), regexpMark);\n        rule.setSourceFile(sourceFile);\n      } else {\n        throw new IllegalStateException(\"Neither '<pattern>' tokens nor '<regexp>' is set in rule '\" + id + \"'\");\n      }\n      setRuleFilter(filterClassName, filterArgs, rule);\n      prepareRule(rule);\n      rules.add(rule);\n    } else {\n      PatternToken patternToken = elemList.get(numElement);\n      if (patternToken.hasOrGroup()) {\n        // When creating a new rule, we finally clear the backed-up variables. All the elements in\n        // the OR group should share the values of backed-up variables. That's why these variables\n        // are backed-up.\n        List<Match> suggestionMatchesBackup = new ArrayList<>(suggestionMatches);\n        List<Match> suggestionMatchesOutMsgBackup =  new ArrayList<>(suggestionMatchesOutMsg);\n        int startPosBackup = startPos;","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleHandler.java#L776-L812","documentation":"Regex-based rules (<regexp>) do not support anti-patterns, unlike token-pattern rules. If any rule- or rulegroup-level <antipattern> elements are present when building a RegexPatternRule, createRules throws this RuntimeException.","triggerScenarios":"A rule whose body uses <regexp> instead of <pattern> while the rule or its enclosing <rulegroup> contains one or more <antipattern> elements.","commonSituations":"Converting a token-based rule to a regex rule without removing its antipatterns; adding antipatterns to a group that also contains a regexp rule.","solutions":["Remove the <antipattern> element(s) from the regexp rule / rulegroup","Rewrite the exclusion logic directly into the regular expression (e.g. negative lookahead)","Convert the rule back to token-based <pattern> form if antipatterns are essential"],"exampleFix":"// before\n<rule id=\"X\"><antipattern><token>ok</token></antipattern><regexp>...</regexp></rule>\n// after\n<rule id=\"X\"><regexp>(?!ok\\b)...</regexp></rule>","handlingStrategy":"validation","validationCode":"// Reject combination of regexp and antipattern\nNodeList rx = doc.getElementsByTagName(\"regexp\");\nif (rx.getLength() > 0 && (doc.getElementsByTagName(\"antipattern\").getLength() > 0))\n  throw new IllegalStateException(\"<regexp> with <antipattern> is unsupported\");","typeGuard":null,"tryCatchPattern":"try { loader.parse(ruleXml, lang); } catch (RuntimeException e) { if (e.getMessage().contains(\"cannot be used together with <antipattern>\")) { log.error(\"Drop antipatterns or convert to pattern rule\"); } throw e; }","preventionTips":["Check the feature matrix before mixing <regexp> with group-level features","Express exclusions in the regex itself (negative lookahead) for regexp rules","Add a CI check that regexp rules contain no antipatterns"],"tags":["xml","regexp","antipattern","unsupported-operation","languagetool"],"backgroundTag":"unsupported-operation","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}