关于 regex
面向 AI Agent 的 MCP 服务,可测试、解释、提取和替换正则匹配,并提供常见文本转换。它返回结构化匹配信息,也支持在一次调用中串联多种转换。
主要功能
- 01测试模式并返回匹配详情
- 02解释正则组件和标志
- 03提取匹配项及捕获组
- 04使用捕获引用替换文本
- 05串联常见文本转换
适用场景
- 01检查模式与示例文本的匹配
- 02理解不熟悉的正则表达式
- 03提取或替换文本匹配项
- 04规范化并整理文本
MCP 相关
向 MCP 客户端提供正则和文本处理工具,包括正则测试、提取、替换与解释。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
5收录的工具列表 · 更新于 3 周前
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| text | string | 是 | 要针对其测试模式的文本 |
| flags | string | 否 | 正则表达式标志(例如,'gi' 表示全局且不区分大小写)。默认为 'g' |
| pattern | string | 是 | 要测试的正则表达式模式 |
详细介绍
mcp-regex-tools
通过模型上下文协议(MCP)为 AI Agent 提供正则表达式和文本处理工具。
工具
| 工具 | 描述 |
|---|---|
regex_test | 测试正则表达式模式与文本的匹配情况。返回所有匹配项的索引、捕获组和命名组。 |
regex_replace | 使用正则表达式查找并替换,支持引用捕获组($1、$2、$&、$<name>)。 |
regex_extract | 将文本中模式的所有匹配项提取为结构化数组。也可以选择只返回特定捕获组。 |
regex_explain | 解析正则表达式模式,并以通俗易懂的方式说明各个组成部分和标志。 |
text_transform | 应用常见的文本转换:camelCase、snake_case、kebab-case、PascalCase、UPPER、lower、标题格式、反转、去除首尾空白、去重行、排序行、删除空行、统计单词/字符/行数。一次调用可串联多个转换。 |
设置
npm install
npm run build
在 Claude Desktop 中使用
将以下内容添加到 Claude Desktop 配置文件中:
- Windows:
%APPDATA%\Claude\claude_desktop_config.json - macOS:
~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"regex-tools": {
"command": "node",
"args": ["D:/products/mcp-servers/mcp-regex-tools/dist/index.js"]
}
}
}
在 Claude Code 中使用
claude mcp add regex-tools node D:/products/mcp-servers/mcp-regex-tools/dist/index.js
或者添加到 .mcp.json:
{
"mcpServers": {
"regex-tools": {
"command": "node",
"args": ["D:/products/mcp-servers/mcp-regex-tools/dist/index.js"]
}
}
}
示例
测试正则表达式
regex_test({ pattern: "(\\d{4})-(\\d{2})-(\\d{2})", text: "Today is 2026-03-19 and tomorrow is 2026-03-20" })
使用捕获组进行替换
regex_replace({ pattern: "(\\w+)\\s(\\w+)", replacement: "$2 $1", text: "hello world" })
提取匹配项
regex_extract({ pattern: "[\\w.]+@[\\w.]+", text: "Contact us at [email protected] or [email protected]" })
说明正则表达式
regex_explain({ pattern: "^(?:https?://)?(?:www\\.)?([\\w-]+)\\.\\w{2,}$", flags: "i" })
转换文本
text_transform({ text: "hello world example", transforms: ["PascalCase"] })
text_transform({ text: "some text\nwith\nlines\nwith\nduplicates\nlines", transforms: ["deduplicate_lines", "sort_lines"] })
许可证
MIT
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。