D2 Regex Replace
Regex text cleanup that doesn't fight your comma-separated tags
- text
Regex replacement in a node graph is usually a miserable affair - either you're pasting Python one-liners into a text box or juggling a separate script. D2 Regex Replace makes it tolerable by splitting the two cases people actually hit: fixing tags in a comma-separated prompt, or rewriting a whole block of text. It's a small node, but if you spend any time cleaning prompts or Danbooru-style tag lists, it'll earn its place fast.
The two modes
The mode dropdown splits the world in two, and the distinction matters:
- Tag - the text is treated as a tag list (comma or newline separated). Each tag gets the replacements applied individually, empty results are dropped, and the surviving tags are rejoined with ", ". This is the mode for "swap
1girl→2girlsacross a messy tag soup" without wrecking the structure. - Advanced - the whole text is run through
re.subwithDOTALL | MULTILINE, once per replacement pair. This is the mode for rewriting entire prompt blocks or removing every occurrence of something regardless of boundaries.
So the same node serves both "clean my tags" and "rewrite my text," which is a nicer split than most regex tools offer.
The replacement syntax
The regex_replace box takes search/replace pairs separated by --. One pair per --, search first, replacement second:
1girl--2girls
masterpiece--best quality
Each pair is applied in order, and the search pattern is a real Python regex, so \b, ^, $ and capture groups all work. The -- separator is the one thing to remember - forget it and your "search" and "replace" get concatenated into one nonsense pattern. If you ever get an "Regex error in tag" message, that's what happened; check the pair formatting before you question the node.
Inputs
text- the input string, multiline. Can be wired from another text node.mode- Tag or Advanced, as above.regex_replace- thesearch--replacepairs.
One output: text, the processed string. If either input is empty it passes the text through untouched, which keeps it safe to leave in a pipeline while you're still building the regex.
Where it fits
The classic combo is feeding it the positive output of D2 Prompt (or D2 Load Image) and normalizing whatever comes out - swapping underscores to spaces, dropping a tag you never want, renaming a character across a folder of captions. It also slots neatly before an XY plot's prompt input when you want the same cleanup applied to every row. It doesn't do anything you couldn't do with a regex node elsewhere, but it does it with a tag-aware mode that actually understands prompt structure, which is more than most generic text tools bother with.
Installing
Part of D2 Nodes ComfyUI:
cd ComfyUI/custom_nodes
git clone https://github.com/da2el-ai/D2-nodes-ComfyUI
or search "D2 Nodes ComfyUI" in ComfyUI Manager. No models, no extra deps beyond the pack's two light requirements.
One honest caveat: Python's regex engine is powerful and the error messages here are… real Python error messages. If you're new to regex, start in Tag mode with simple literal swaps - masterpiece--best quality needs no regex knowledge at all. Save the capture groups for after you've watched it work once.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| mode | COMBO | 2 options: Tag, Advanced | |
| regex_replace | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| text | STRING | — |