Provides regex pattern matching, validation, and replacement operations for text processing.
ⓘ This plugin is a sub-package of the omdsh-dev/dsh-toolkit monorepo — stars and activity count the whole repository.
- Language
- TypeScript
- License
- MIT
- Branch
- main
Install
$ dsh plugin --profile web add github:omdsh-dev/dsh-toolkit#path:packages/dsh-tool-regexRun the command above in your terminal to install this plugin via the dsh CLI. You can switch Profile in the top-right corner. New to dsh? Read the beginner tutorial
Install via your agent
Install the DeepSeek Harness plugin omdsh-dev/dsh-toolkit/packages/dsh-tool-regex for me: review the repository at https://github.com/omdsh-dev/dsh-toolkit first, then run the install command and verify the plugin loads successfully.
Paste this instruction to the DSH Web GUI assistant — it will install and verify for you.
一句话定位
dsh-tool-regex 为 DSH 助手提供一组零依赖的正则操作:测试匹配、提取捕获组、安全替换,并能静态解释正则含义(不执行代码)。它把易出错的"心算正则"换成可验证、可限时的工具调用,从源头规避灾难性回溯(ReDoS)阻塞宿主。
核心能力
- 测试文本是否匹配给定正则(test),整串语义由调用方用
^...$锚点表达 - 提取文本中所有匹配的位置、完整匹配、编号捕获组与命名捕获组(find,未传 g 时自动补 g)
- 对文本执行全局安全替换,支持
$1/$<name>/$$等原生引用语法(replace) - 静态解析正则结构,把 pattern 拆成人读节点序列输出,不构造 RegExp、不执行匹配(explain)
- 多层资源防护:输入/pattern/replacement/输出字节上限 + find 匹配数钳制 + worker 硬超时
技术实现
- 语言: TypeScript(零运行时依赖,纯函数 + Node 内置
node:worker_threads) - 关键依赖:
node:worker_threads(隔离执行线程)、@deepseek-ai/dsh-tools(defineTool工具定义)、@deepseek-ai/cordis(宿主注入) - 架构模式: 通过包内
cordis.patch.yml将插件插入 profile 的 layer stack(row idtool-regex),注册名为regex的工具;test/find/replace派发到Worker线程执行(WORKER_BUDGET_MS = 1000ms超期强制terminate()),explain走主线程同步静态 tokenizer - 入口文件:
src/index.ts(apply(ctx)钩子)
适用场景
当模型需要验证一条正则、解析日志字段、抽取捕获组或执行文本替换时使用本插件,避免模型"心算"正则出错率高、用户不可验证的痛点。它尤其适合处理用户给出的 pattern(先 explain 让用户看清含义,再 test/find 验证),以及需要 $1/$<name> 引用语法的批量重写场景。
前置依赖与兼容性
| 依赖 | 最低版本 | 说明 |
|---|---|---|
| DSH | 0.1.0-rc.8 | 已在 @deepseek-ai/[email protected] 隔离 consumer 中完成全链路验证 |
| Node.js | 22.19.0 或 ≥24.0.0 | engines.node 声明 ^22.19.0 || >=24.0.0 |
| @deepseek-ai/cordis | ^4.0.1 | peer 依赖,由宿主提供 |
| @deepseek-ai/dsh-tools | ≥0.0.1-rc.1 <0.2.0 | peer 依赖,提供 defineTool |
| @deepseek-ai/dsh-invariants | ≥0.0.1-rc.1 <0.2.0 | peer 依赖 |
| 平台 | 跨平台 | 无 os/cpu 字段限制 |
| 原生模块 | node:worker_threads | Node 内置,无需额外编译 |
安装方式
dsh plugin --profile web add github:omdsh-dev/dsh-toolkit/packages/dsh-tool-regex
配置项
本插件无需额外配置。安装后插件自动注册 regex 工具,无需读取配置文件或环境变量。所有可调参数均通过工具调用本身的 flags、replacement、limit 字段在每次调用时传入。
常见问题
Q: 这个插件需要任何配置吗?
A: 不需要。安装完成后插件会自动注册到 profile 层,工具即可直接调用,无需编辑配置文件或设置环境变量。
Q: 和系统的 grep 命令有什么区别?
A: grep 是文件域搜索且需启动 shell 进程;本插件接受任意文本字符串作为输入,可以提取捕获组、执行替换,还能静态解释正则结构。速度更快、不依赖文件、不污染宿主环境。
Q: 病理正则(如 (a+)+)会不会卡死助手?
A: 不会。test/find/replace 跑在独立 worker 线程中,1000ms 到期即强制 terminate() 并返回错误;explain 完全不构造 RegExp、走静态扫描,天然免疫回溯。
Q: 输入大小有上限吗?
A: 有。入口硬上限:待匹配文本 ≤ 64KB、pattern ≤ 16KB、replacement ≤ 16KB、输出 ≤ 1MB、find 报告匹配数 ≤ 1000。超限会直接拒绝(不截断)。
Q: explain 动作和其他三个动作有什么不同?
A: explain 只把正则字符串拆成节点(字面量、字符类、量词、锚点等),返回人读解释,完全不执行匹配;test/find/replace 则会真实跑匹配并返回结果。explain 是验证"这条正则到底长什么样"的安全手段。
Q: replace 支持哪些替换引用语法?
A: 支持 JS 原生 $-语义:$$ → 字面 $,$1/$2 → 编号捕获组,$<name> → 命名捕获组。未知引用按 V8 行为字面保留。实现走 String.replace 的字符串替换路径,不会执行代码。
Q: 怎么卸载?
A: 通过 profile 的 bundle 层管理移除:在对应 profile 中删除 row id tool-regex,或使用 dsh plugin --profile web remove 命令。
Q: 安装命令中的 --profile web 是什么意思?
A: DSH 用 profile 区分交互式(web)和一次性任务(headless)环境。web 用于交互式 UI,headless 是 dsh run 的默认 profile。两者独立,需要分别安装。
上手难度
入门 — 调用接口只需传 action + pattern + input 三参数,所有安全边界和超时已内置,无需任何额外配置或学习成本。
已知问题与限制
- 资源硬上限:输入 64KB / pattern 16KB / replacement 16KB / 输出 1MB / find 匹配数 1000,超限会被拒绝(
src/engine.ts:22-27) - find 的
limit默认 50,最多 1000(src/engine.ts:30,67),超出钳制而非报错 test/find/replace的总执行时间硬上限 1000ms(src/index.ts:27),超时返回错误而非截断结果- 对"无锚点的嵌套量词 + 不可信大输入"组合存在原生 ReDoS 风险,README 明确要求模型对此类 pattern 保持警惕(
README.md:24) explain节点数 ≤ 4096,超限返回regex: explain: pattern too complex(src/engine.ts:27)
DSH 零依赖工具包 collection —— time / encoding / json / calculator / csv / regex / markdown / diff / stat / schema 十个确定性工具,统一入口一键安装。
为什么
DSH 生态仓库持续增长,单插件在 hub 中容易被淹没;collection 分类是辨识度最高的形态。本仓库把 10 个工具插件 vendored 冻结为 pack artifact 快照(各子仓库独立演进,当前源位于 omdsh-dev 组织下),统一工程、统一测试、统一维护。
定位(官方 Profile Bundle 生态方向):本仓库是 collection 与安装辅助仓库——每个子包都是可独立安装/启用/禁用/卸载的 bundle(dsh plugin --profile <p> add <子包>);collection 提供目录、清单与批量安装脚本。meta 包 @deepseek-ai/dsh-toolkit 保留为可选的原子挂载模型(见下文两种运行模型)。
分发边界:根 meta 包保持 private: true,用于 Git/collection 分发,不代表会发布到 npm registry。根目录已提交由当前 src 构建出的 lib/index.js 与 lib/types/index.d.ts,因此从 Git 安装时不依赖消费端 lifecycle;prepack 仍会在生成 pack artifact 前执行完整构建。
工具一览
| 工具 | 能力 | 用例数 |
|---|---|---|
time | ISO 8601 / 时区 / 日历运算 / 时长差 | 65 |
encoding | base64 / url / hex / hash / UUID | 46 |
json | JMESPath 子集查询 | 66 |
calculator | 安全数学表达式求值(无 eval) | 31 |
csv | RFC 4180 解析 / 查询 / 统计(严格引号) | 50 |
regex | 测试 / 提取 / 替换 / 静态解释(worker 硬超时) | 63 |
markdown | HTML↔Markdown / GFM 表格 / 目录生成(白名单安全) | 71 |
diff | 文本/JSON/CSV/Markdown 结构化比较与 unified diff(只读) | 124 |
stat | 描述统计 / 百分位数 / 频数分布 / 相关性(零依赖确定性) | 82 |
schema | JSON Schema 验证 / 路径 / 解释 / 安全 default(零网络零动态) | 125 |
| 合计 | 723 |
架构
dsh-toolkit/
├── src/index.ts # meta 包:相对路径动态导入 10 个子包 apply(),聚合注册
├── packages/dsh-tool-* # vendored 子包(pack artifact 快照,name 保持 @deepseek-ai/dsh-tool-*)
├── scripts/
│ ├── link-deps.sh # 构建期 junction(cordis → vendor/cordis,dsh-tools → packages/core/tools)
│ ├── build-all.sh # 一键构建 10 子包 + meta 包(tsc)
│ ├── test-all.sh # 一键跑 10 子包 vitest(合计用例数)
│ ├── install.sh # meta / 逐包两种挂载模式(含 dry-run)
│ ├── install-web.sh # 独立 bundle 批量安装 → web profile
│ ├── install-headless.sh # 独立 bundle 批量安装 → headless profile
│ └── install-all.sh # web + headless 都装
├── catalog.json # collection 清单(hub collection 分类识别依据)
└── tsconfig.base.json # 共享编译配置(固化踩坑经验)
与实施文档方案 A 的工程化适配:子包为私有 Git/collection 包,peer 名解析在 profile 内不可行, 故 meta 包采用相对路径动态导入(零解析魔法、打包自足);子包 runtime 依赖 (
@deepseek-ai/dsh-tools)在 npm 独立模式(默认)下不依赖 DSH monorepo,monorepo 模式经子包构建期 junction 解析。
安装
仓库位于 omdsh-dev/dsh-toolkit(public)。
独立 bundle 模型(推荐)
每个子包独立安装、启用、禁用、卸载:
# 安装单个工具到 web profile
dsh plugin --profile web add github:omdsh-dev/dsh-tool-csv
# 一次性任务(headless)profile
dsh plugin --profile headless add github:omdsh-dev/dsh-tool-diff
批量安装(collection 辅助脚本,幂等——重复执行不会重复添加):
./scripts/install-web.sh # 全部 10 工具 → web profile
./scripts/install-headless.sh # 全部 10 工具 → headless profile(dsh run 使用面)
./scripts/install-all.sh # 两个 profile 都装
验证与运行:
dsh --profile web --dump-config | grep tool-csv # 行存在即安装成功
dsh run "使用 csv 工具解析 'a,b\n1,2'" # headless 端到端
⚠️ web 与 headless 是不同 profile:web 安装不会自动覆盖 headless;
dsh run默认使用 headless。 Windows 路径使用正斜杠(C:/...)。
npm pack tarball 安装
本地构建后用 tarball 路径安装(不依赖 GitHub):
# tarball 方式(web 为例;headless 同)
npm pack
dsh plugin --profile web add <npm pack 产物 tarball 路径>
meta bundle 模型(可选)
需要一次原子挂载全部工具时,挂载根 meta 包:
dsh plugin --profile web add github:omdsh-dev/dsh-toolkit
dsh --profile web --dump-config | grep tool-kit
⚠️ 若 profile 已单独挂载过同名插件(tool-time/.../tool-diff/tool-stat/tool-schema),挂 meta 包会注册重名报错—— 此时先移除旧插件,或用独立 bundle 模型。meta apply 具备原子性(任一子插件失败时 逆序回滚已注册工具,不残留部分状态)。
手动安装与旧版本兼容
旧场景(monorepo 集成、不支持 Profile Bundle 的旧快照或插件开发调试环境——本地 junction/symlink、手动编辑 profile 层)。
构建与测试
npm 独立模式(默认,推荐):无需 DSH monorepo;npm install(devDependencies 自包含)后即可:
npm run build:all # 10 子包 + meta 包 tsc + 产物完整性验证(无 .ts 残留导入、10+1 个 lib/index.js)
bash scripts/test-all.sh # 10 子包 vitest 全量(723 用例);任一失败整体非零退出
npm pack # prepack 自包含(build:all),tarball 含 lib + 10 个子包
monorepo 模式(可选:源码贡献/旧 snapshot):显式提供 DSH monorepo 根:
export DSH_MONOREPO=<DSH 0.1.0-rc.8(npm)安装路径> # 或作为第一个参数
bash scripts/build-all.sh # link-deps + 10 子包 + meta 包 tsc + 产物完整性验证
bash scripts/test-all.sh
prepack已指向build:all(完整构建 10 子包 + meta),保证 pack artifact 含全部运行入口;根 Git 入口由当前src预构建并提交。 本仓库只在本地验证构建、测试与 pack artifact;不要将这些结果解读为 npm registry 发布或未实际执行的 consumer/profile 验证。
同步 vendored(子仓库有更新时)
把 packages/<name> 与源仓库重新同步(src/tests/package.json/tsconfig/cordis.patch.yml/LICENSE/README.md),并在 README 标注子包版本。
新增工具
见 docs/CONTRIBUTING.md:复制模板子包 → 实现 → 测试 → build-all 验证 → 更新 catalog.json。
许可
MIT
Read the usage guide →
Install steps, key points, FAQ and compatibility for this plugin — auto-derived from indexed fields.
Listing badge
[](https://deepseek-plugin.org/plugins/omdsh-dev/dsh-toolkit/packages/dsh-tool-regex)Paste this markdown into your GitHub README to link back to this listing. The badge only states the listing — not a security endorsement.