{"record":{"id":"b3b7c2a6d5ac556e","repo":"json-path/JsonPath","slug":"could-not-find-matching-close-for-s-when-parsing","errorCode":null,"errorMessage":"Could not find matching close for %s when parsing regex in : %s","messagePattern":"Could not find matching close for (.+?) when parsing regex in : (.+?)","errorType":"validation","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java","lineNumber":112,"sourceCode":"\n        int opened = 1;\n        int readPosition = startPosition + 1;\n        while (inBounds(readPosition)) {\n            if (skipStrings) {\n                char quoteChar = charAt(readPosition);\n                if (quoteChar == SINGLE_QUOTE || quoteChar == DOUBLE_QUOTE){\n                    readPosition = nextIndexOfUnescaped(readPosition, quoteChar);\n                    if(readPosition == -1){\n                        throw new InvalidPathException(\"Could not find matching close quote for \" + quoteChar + \" when parsing : \" + charSequence);\n                    }\n                    readPosition++;\n                }\n            }\n            if (skipRegex) {\n                if (charAt(readPosition) == REGEX) {\n                    readPosition = nextIndexOfUnescaped(readPosition, REGEX);\n                    if(readPosition == -1){\n                        throw new InvalidPathException(\"Could not find matching close for \" + REGEX + \" when parsing regex in : \" + charSequence);\n                    }\n                    readPosition++;\n                }\n            }\n            if (charAt(readPosition) == openChar) {\n                opened++;\n            }\n            if (charAt(readPosition) == closeChar) {\n                opened--;\n                if (opened == 0) {\n                    return readPosition;\n                }\n            }\n            readPosition++;\n        }\n        return -1;\n    }\n","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java#L94-L130","documentation":"indexOfMatchingCloseChar also supports regex literals inside path filters (a leading slash opens a regex region, detected when skipRegex is true). It uses nextIndexOfUnescaped to locate the closing unescaped slash; if it reaches the end of the input without finding one, it throws InvalidPathException because the regex literal is never terminated.","triggerScenarios":"Evaluating a filter path containing a regex pattern like /[.../ or ~regex segments where the closing slash was omitted or escaped incorrectly, e.g. \"$[?(@.name =~ /abc)]\" with the trailing slash deleted.","commonSituations":"Hand-written regex filters where the trailing slash is lost during editing; regex patterns built programmatically where the source pattern already contains slashes or escapes; mistaking a regex character class containing a slash for a terminator.","solutions":["Add the missing closing slash to terminate the regex literal in the path.","Escape any literal slashes inside the regex with a backslash (\\/) so they are not mistaken for the terminator.","Pre-test the regex pattern standalone before embedding it into the filter expression.","Use JsonPath.compile() on the generated path early to surface the syntax error at a well-defined point."],"exampleFix":"// before\nString path = \"$[?(@.url =~ /https://example)]\"; // unescaped slashes end the regex early / leave it open\n// after\nString path = \"$[?(@.url =~ /https:\\\\/\\\\/example/)]\";","handlingStrategy":"validation","validationCode":"boolean regexOpen = false;\nfor (int i = 0; i < path.length(); i++) {\n    if (path.charAt(i) == '/' && (i == 0 || path.charAt(i - 1) != '\\\\')) regexOpen = !regexOpen;\n}\nif (regexOpen) throw new IllegalArgumentException(\"Unterminated regex literal in path: \" + path);\nJsonPath.compile(path);","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.compile(path);\n} catch (InvalidPathException e) {\n    if (e.getMessage().contains(\"regex\")) {\n        throw new IllegalArgumentException(\"Regex literal in path is not terminated: \" + path, e);\n    }\n    throw e;\n}","preventionTips":["Escape literal slashes inside regex filters as \\/","Test the regex pattern in isolation before embedding it","Build regex filters from a helper that guarantees surrounding slashes","Never hand-edit the closing slash out of =~ /pattern/ filters"],"tags":["json-path","path-parsing","regex-literal","invalid-path"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}