{"record":{"id":"294e2bdc07be3ef2","repo":"karatelabs/karate","slug":"extract-needs-three-arguments-text-regex-group","errorCode":null,"errorMessage":"extract() needs three arguments: text, regex, group","messagePattern":"extract\\(\\) needs three arguments: text, regex, group","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":148,"sourceCode":"     * Usage: karate.distinct([1, 2, 2, 3, 1]) => [1, 2, 3]\n     */\n    static JavaInvokable distinct() {\n        return args -> {\n            if (args.length == 0 || args[0] == null) {\n                return new ArrayList<>();\n            }\n            if (!(args[0] instanceof List<?> list)) {\n                return new ArrayList<>();\n            }\n            Set<Object> seen = new LinkedHashSet<>(list);\n            return new ArrayList<>(seen);\n        };\n    }\n\n    static JavaInvokable extract() {\n        return args -> {\n            if (args.length < 3) {\n                throw new RuntimeException(\"extract() needs three arguments: text, regex, group\");\n            }\n            String text = args[0].toString();\n            String regex = args[1].toString();\n            int group = ((Number) args[2]).intValue();\n            Pattern pattern = Pattern.compile(regex);\n            Matcher matcher = pattern.matcher(text);\n            if (!matcher.find()) {\n                return null;\n            }\n            return matcher.group(group);\n        };\n    }\n\n    static JavaInvokable extractAll() {\n        return args -> {\n            if (args.length < 3) {\n                throw new RuntimeException(\"extractAll() needs three arguments: text, regex, group\");\n            }","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L130-L166","documentation":"Karate's extract() JS utility applies a regex to text and returns one capture group from the first match. It requires exactly the trio text, regex, and group index; calling it with fewer than three arguments throws this error.","triggerScenarios":"Calling extract(text, regex) omitting the group index, extract(text) with only the text, or extract() with no arguments.","commonSituations":"Porting from other regex helpers that default to group 0; forgetting that group is 1-based here; trimming arguments while debugging.","solutions":["Pass all three arguments: extract(text, 'pattern', 1)","Use group 0 if you want the whole match","If the regex itself is the problem, verify it compiles before blaming the argument count"],"exampleFix":"// before\nvar id = karate.extract(responseBody, 'id=(\\\\d+)');\n// after\nvar id = karate.extract(responseBody, 'id=(\\\\d+)', 1);","handlingStrategy":"validation","validationCode":"if (text == null || regex == null || group == null) throw new Error('extract requires text, regex and group');","typeGuard":"function canExtract(args) { return args.length >= 3 && typeof args[1] === 'string' && typeof args[2] === 'number'; }","tryCatchPattern":"try { id = karate.extract(text, regex, 1); } catch (e) { karate.log('extract failed: ' + e.message); }","preventionTips":["Never omit the group index — there is no default","Group indexes are 1-based; use 0 for the whole match","Test regexes with a small fixture before use in scenarios"],"tags":["argument-count","regex","javascript","karate"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}