regex_test
Test regular expression patterns against input strings to validate matches, extract groups, and identify positions within text for debugging and development.
Instructions
Test a regular expression pattern against an input string. Returns all matches with groups and indices.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | The regex pattern (without delimiters) | |
| flags | No | Regex flags (default: 'g') | g |
| input | Yes | The string to test against |
Implementation Reference
- src/tools/text.ts:205-271 (handler)Implementation of the regex_test tool, which tests a regular expression against a string.
server.tool( "regex_test", "Test a regular expression pattern against an input string. Returns all matches with groups and indices.", { pattern: z.string().describe("The regex pattern (without delimiters)"), flags: z .string() .default("g") .describe("Regex flags (default: 'g')"), input: z.string().describe("The string to test against"), }, async ({ pattern, flags, input }) => { try { const regex = new RegExp(pattern, flags); const matches: Array<{ match: string; index: number; groups: Record<string, string> | null; }> = []; let m; if (flags.includes("g")) { while ((m = regex.exec(input)) !== null) { matches.push({ match: m[0], index: m.index, groups: m.groups ? { ...m.groups } : null, }); if (matches.length > 100) break; // Safety limit } } else { m = regex.exec(input); if (m) { matches.push({ match: m[0], index: m.index, groups: m.groups ? { ...m.groups } : null, }); } } const result = { pattern, flags, input_length: input.length, matches_found: matches.length, matches, }; return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2) }, ], }; } catch (e) { return { content: [ { type: "text" as const, text: `Error: Invalid regex — ${e instanceof Error ? e.message : String(e)}`, }, ], isError: true, }; } } );