karatelabs/karate · error · RuntimeException
extract() needs three arguments: text, regex, group
Error message
extract() needs three arguments: text, regex, group
What it means
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.
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
Example fix
// before var id = karate.extract(responseBody, 'id=(\\d+)'); // after var id = karate.extract(responseBody, 'id=(\\d+)', 1);
Defensive patterns
Strategy: validation
Validate before calling
if (text == null || regex == null || group == null) throw new Error('extract requires text, regex and group'); Type guard
function canExtract(args) { return args.length >= 3 && typeof args[1] === 'string' && typeof args[2] === 'number'; } Try / catch
try { id = karate.extract(text, regex, 1); } catch (e) { karate.log('extract failed: ' + e.message); } Prevention
- 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
When it happens
Trigger: Calling extract(text, regex) omitting the group index, extract(text) with only the text, or extract() with no arguments.
Common situations: Porting from other regex helpers that default to group 0; forgetting that group is 1-based here; trimming arguments while debugging.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- extractAll() needs three arguments: text, regex, group
- eval() needs one argument
- expect() needs at least one argument
- append() needs at least two arguments
- appendTo() needs at least two arguments: list and item(s)
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/294e2bdc07be3ef2.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:148
* Usage: karate.distinct([1, 2, 2, 3, 1]) => [1, 2, 3]
*/
static JavaInvokable distinct() {
return args -> {
if (args.length == 0 || args[0] == null) {
return new ArrayList<>();
}
if (!(args[0] instanceof List<?> list)) {
return new ArrayList<>();
}
Set<Object> seen = new LinkedHashSet<>(list);
return new ArrayList<>(seen);
};
}
static JavaInvokable extract() {
return args -> {
if (args.length < 3) {
throw new RuntimeException("extract() needs three arguments: text, regex, group");
}
String text = args[0].toString();
String regex = args[1].toString();
int group = ((Number) args[2]).intValue();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (!matcher.find()) {
return null;
}
return matcher.group(group);
};
}
static JavaInvokable extractAll() {
return args -> {
if (args.length < 3) {
throw new RuntimeException("extractAll() needs three arguments: text, regex, group");
}View on GitHub (pinned to a22eb90246)