Substitute Tags
Regex prompt surgery that only fires when it should
- processed_text
LLM prompt helpers and TIPO-style taggers are great until they aren't - they hand back prompts that are almost right, with one or two tags you always have to fix: a 1boy when you want 1girl, a stray solo, a quality tag you strip every time. Substitute Tags is a regex find-and-replace with two guard rails, run_if and skip_if, so your cleanup rule only fires when the prompt actually needs it.
Why you'd reach for it
A plain replace is useless for cleanup because the fix depends on context. The canonical example from the README: you want 1boy turned into 1girl, 1boy, but only when the word girl isn't already in the prompt - otherwise you'd end up with 1girl, 1girl, 1boy. With skip_if: girl, the rule is safe to leave in a workflow permanently. Same trick works in reverse with run_if: only apply the substitution when some other tag is present. That's the difference between a fragile regex you babysit and a cleanup rule you can forget about.
How it works
Nothing exotic under the hood - it's Python's re.sub with two conditional gates in front:
- If
run_ifis set and its pattern is not found in the text, the text is returned untouched. - If
skip_ifis set and its pattern is found, the text is returned untouched. - Otherwise,
pattern→replsubstitution happens (all occurrences, not just the first).
Because the gates and the pattern are all regex, the same node handles exact strings, word boundaries, or sloppy-matching patterns.
Inputs and outputs
- text - the prompt to process.
- pattern - the regex to match.
- repl - the replacement string (regex backreferences like
\1work). - run_if (optional) - only run the substitution if this pattern exists.
- skip_if (optional) - skip the substitution if this pattern exists.
One output: processed_text.
A couple of the author's own examples to steal:
# If "girl" is absent, upgrade 1boy to 1girl, 1boy
pattern: 1boy
repl: 1girl, 1boy
skip_if: girl
# If 1boy is present, drop a redundant solo tag
pattern: solo,?\s*
repl:
run_if: 1boy
Installation
This node lives in ComfyUI-Alchemine-Pack, along with the rest of the prompt toolbox. Install via ComfyUI Manager (search "Alchemine") and restart, or by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/alchemine/comfyui-alchemine-pack
pip install -r requirements.txt
Restart ComfyUI. Only dependency worth mentioning is python-dotenv; these prompt nodes download nothing and need no API keys.
Common issues
The usual regex gotchas apply, and they're worth stating plainly since beginners hit all three. Parentheses and dots are regex metacharacters - pattern: 1girl (smile) won't match a literal (smile) unless you escape it (\(smile\)). Both gates run a search, so skip_if: girl also catches 1girl (that's why the README example works). And since substitution replaces every match, a greedy pattern can eat more than you intended - test on a text-preview node before trusting it in a queue. There's no debug mode, but the node's own example usage in the source is a good template to copy from.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| pattern | STRING | — | |
| repl | STRING | — | |
| run_ifopt | STRING | — | |
| skip_ifopt | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| processed_text | STRING | — |