find_regex_matches
Locate all regular expression matches with character positions in text. Returns start, end, and match details for precise text analysis and extraction.
Instructions
Find all regex matches with positions. Returns list of {start, end, match} dicts.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| pattern | Yes |
Implementation Reference
- char_index_mcp/server.py:188-205 (handler)The main handler function that implements the 'find_regex_matches' tool logic. It uses Python's re.finditer to locate all non-overlapping matches of the given regex pattern in the text and returns a list of dictionaries containing the start index, end index, and matched string for each match. Includes error handling for invalid regex patterns.@mcp.tool() def find_regex_matches( text: Annotated[str, "Text to search in"], pattern: Annotated[str, "Regular expression pattern"] ) -> list[dict]: """Find all regex matches with positions. Returns list of {start, end, match} dicts.""" try: matches = [] for match in re.finditer(pattern, text): matches.append({ "start": match.start(), "end": match.end(), "match": match.group() }) return matches except re.error as e: raise ValueError(f"Invalid regex pattern: {e}")
- char_index_mcp/server.py:188-188 (registration)The @mcp.tool() decorator registers the find_regex_matches function as an MCP tool.@mcp.tool()
- char_index_mcp/server.py:190-192 (schema)Input schema defined via type annotations: text (str), pattern (str). Output: list[dict] with match details.text: Annotated[str, "Text to search in"], pattern: Annotated[str, "Regular expression pattern"] ) -> list[dict]: