批量文本替换
Find-and-replace across a whole list of strings, with regex auto-detection
- 替换后列表
Boring, unglamorous, and quietly the most useful text node in this pack. BatchTextReplace takes a list of strings - captions, prompts, filenames - and runs up to three find-and-replace rules across every one of them, outputting the transformed list. That's it. No AI, no model, no API. It's the batch-edit step you'd otherwise do in a text editor, except it lives inside the graph so it happens automatically every run.
Where it earns its keep is training-data cleanup. If you've ever prepped a LoRA dataset, you know the drill: a folder of images, each with a caption sidecar, and the same mistakes in all of them - a misspelled trigger word, a tag that should be 1girl not one girl, a trailing bit of boilerplate the captioner added to every line. Feed the captions in (this pack's MuyeFileReader can hand you a text list, and SaveFilesToLocal writes them back out), drop in your three rules, and every caption comes out fixed in one pass. The KB's LoRA-training material is full of this pattern: clean, consistent captions matter more than elaborate ones.
The three rules are 查找文本1/2/3 paired with 替换为1/2/3, applied in order - rule 2 sees the output of rule 1, rule 3 sees rule 2's. Leave a 查找文本 blank and that rule is skipped. The one genuinely clever bit: it tries to compile each find string as a regex, and if that succeeds it uses regex substitution; if the compile fails, it falls back to a plain literal str.replace. So you can put ^\d+\. to strip leading numbers, or just badword for a plain swap.
The trap
Regex auto-detection is a double-edged sword and it's the thing that bites. If your find text happens to be a valid regex - and in regex, nearly anything is - it will be treated as one. A literal 1.0 in a caption becomes a regex where . matches any character, so it would happily rewrite 1x0, 1:0, 1 0. If you're replacing strings that contain regex-special characters (., (, [, *, +), you are playing regex roulette whether you meant to or not. When in doubt, test on a single caption first.
The output is 替换后列表, a STRING list, so it daisy-chains into any node that eats a list of strings - or into a text writer to save the cleaned captions back to disk.
Install
ComfyUI Manager → search "muye" → install "ComfyUI-Muye-nodes", restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/muyexiuluo/ComfyUI-Muye-nodes
cd ComfyUI-Muye-nodes
pip install -r requirements.txt
then restart. This node is pure Python standard library - no extra deps - so it works even if the pack's heavier requirements (transformers, uniface) fail to install. The README's manual block shows an older ComfyUI_Muye.git URL; use the -nodes one above. Display name is 批量文本替换, and every field is labeled in Chinese, which is expected, not a bug. If the node's missing, check the console for a [Muye] Failed to load module line.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| 输入文本 | STRING | — | |
| 查找文本1 | STRING | — | |
| 替换为1 | STRING | — | |
| 查找文本2 | STRING | — | |
| 替换为2 | STRING | — | |
| 查找文本3 | STRING | — | |
| 替换为3 | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| 替换后列表 | STRING | — |