mcp-regex mcp

v1.0.0 · MCP Tool · registry.pascalai.org

Match, extract, replace and split text using regular expressions. Supports named capture groups, multiline mode, and Unicode. Use to validate formats, extract structured data from free-form text, or perform pattern-based transformations.

Operations

OperationDescription
matchMatch the pattern anchored at the start of the string.
searchFind the first occurrence of the pattern anywhere in the text.
find_allFind all non-overlapping matches in the text (default operation).
replaceReplace matches with a replacement string. Supports backreferences: $1, $name.
splitSplit the text into an array of substrings at each pattern match.
validateCheck whether the entire string matches the pattern. Returns is_valid.

Input Parameters

ParameterTypeDescription
textrequired string The input text to search or transform.
patternrequired string Regular expression pattern. Supports named groups with (?P<name>...) syntax.
operationoptional string Operation to perform: match, search, find_all, replace, split, validate. Default: "find_all".
replacementoptional string Replacement string for the replace operation. Supports positional backreferences ($1) and named backreferences ($name).
flagsoptional array Array of flag strings: ignorecase, multiline, dotall, unicode, global. Default: ["global"].
max_matchesoptional integer Maximum number of matches to return for find_all. Default: 100.

Output Fields

FieldTypeDescription
matches array Array of match objects. Each has: value (string), start (integer), end (integer), groups (array of positional captures), named_groups (object of named captures).
match_count integer Total number of matches found.
result string Output string for replace (text after substitution) and split (JSON-encoded array of parts).
is_valid boolean For validate: true if the entire text matches the pattern, false otherwise.

Examples

// Extract all email addresses from a text
{
  "text": "Contact: info@example.com and hr@company.org",
  "pattern": "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}",
  "operation": "find_all"
}
// Output
{
  "matches": [
    { "value": "info@example.com", "start": 9,  "end": 25 },
    { "value": "hr@company.org",  "start": 30, "end": 44 }
  ],
  "match_count": 2
}

// Named groups — parse a structured log line
{
  "text": "2026-03-13 15:30:45 ERROR Database timeout",
  "pattern": "(?P<date>\\d{4}-\\d{2}-\\d{2}) (?P<time>\\d{2}:\\d{2}:\\d{2}) (?P<level>\\w+) (?P<msg>.+)",
  "operation": "search"
}
// Output
{
  "matches": [{
    "value": "2026-03-13 15:30:45 ERROR Database timeout",
    "named_groups": {
      "date":  "2026-03-13",
      "time":  "15:30:45",
      "level": "ERROR",
      "msg":   "Database timeout"
    }
  }],
  "match_count": 1
}

// Replace — mask credit card digits
{
  "text": "Card: 4111-1111-1111-1234",
  "pattern": "\\d{4}-\\d{4}-\\d{4}-(?P<last>\\d{4})",
  "operation": "replace",
  "replacement": "****-****-****-$last"
}
// Output: { "result": "Card: ****-****-****-1234" }

Install & Discovery

Install

ppm install mcp-regex

Get JSON Schema

GET /v1/packages/mcp-regex/1.0.0/schema

Discover by keyword

GET /v1/mcp/discover?q=regex+pattern+match
Discovery hint: Use this tool whenever an agent needs to validate input formats (email, phone, ISBN), extract structured data from logs or documents, sanitize text by replacing patterns, or split strings by complex delimiters.

PascalAI Usage

uses toolslib;

var RX := LoadTool('mcp-regex');

// Validate email address format
var V := RX.Call(JsonObj([
  'text',      UserEmail,
  'pattern',   '^[^@]+@[^@]+\.[^@]+$',
  'operation', 'validate'
]));
if not V['is_valid'] then
  Error('Invalid email address');

// Extract all URLs from an HTML document
var Links := RX.Call(JsonObj([
  'text',      HTML,
  'pattern',   'https?://[^\s"<>]+',
  'operation', 'find_all'
]));
for var M in Links['matches'] do
  Writeln(M['value']);