ComfyUI Node

Substitute Tags

Regex prompt surgery that only fires when it should

By alchemine·Created about a year ago·Updated 2 days ago· 2
Substitute Tags
    • processed_text
    text
    pattern
    repl
    run_if
    skip_if

    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:

    1. If run_if is set and its pattern is not found in the text, the text is returned untouched.
    2. If skip_if is set and its pattern is found, the text is returned untouched.
    3. Otherwise, patternrepl substitution 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 \1 work).
    • 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.

    CategoryAlcheminePack/Prompt

    Inputs (5)

    NameTypeDefaultDescription
    textSTRING
    patternSTRING
    replSTRING
    run_ifoptSTRING
    skip_ifoptSTRING

    Outputs (1)

    NameTypeDescription
    processed_textSTRING