Nodes/D2 Nodes ComfyUI/D2 Regex Switcher
ComfyUI Node

D2 Regex Switcher

The node behind per-checkpoint quality tags

By da2el-ai·Created 2 years ago·Updated 30 days ago· 66
D2 Regex Switcher
    • combined_text
    • prefix
    • suffix
    • index
    text
    regex_and_outputpony -- score_9, -- -- highres, high quality,
    pre_delim
    suf_delim
    show_text
    text_check
    prefix
    suffix

    Regex Switcher is the node that powers the D2 pack's headline trick: a workflow that automatically picks the right quality tags based on which checkpoint family you're using. Pony models want score_9, score_8_up up front; Illustrious and others want a different set; and you don't want to hand-swap that text every time you change checkpoints.

    Here's the idea. You give it a target string (say, the prompt), plus a lookup table of regex→output pairs. It searches your text with each regex in order and, on the first match, outputs that pair's text. No match? It falls back to a default output. Same general shape as A1111's wildcard-ish switching, done with regex so the matching can be loose.

    The inputs that matter

    • text - the string being searched. Typically your prompt, wired in from a primitive or the checkpoint's trigger text.
    • regex_and_output - the pair table. Format is regex, --, output, repeated:
      pony
      --
      score_9, score_8_up, source_anime,
      --
      --
      highres, high quality,
      A pair with an empty regex line is the default output (used when nothing matches).
    • pre_delim / suf_delim - the separator between your prefix/suffix and the matched output: Comma, Line break, or None.
    • prefix / suffix - optional text stitched onto the front and back of the result.
    • text_check - a scratch field for testing your regexes against before you commit.

    Outputs: combined_text (prefix + separator + matched output + separator + suffix), prefix and suffix (passthroughs), and index - the 0-based position of the matching rule, or -1 when nothing matched. That index is genuinely useful: it lets downstream nodes branch on which family you're in, not just the text.

    The mechanism, worth knowing

    It's first-match-wins with re.search and case-insensitivity. The order of your pairs is the priority order - put the most specific regex first, or the generic one will eat the match. The default table in the node already demonstrates the pattern: match pony → pony quality tags, else → generic highres tags. Watch that the empty-regex default can be anywhere; the parser treats a pair with an empty regex line as the fallback.

    Installing

    Ships in the D2-nodes-ComfyUI pack - install the pack, not the node.

    • ComfyUI Manager → search "D2-nodes-ComfyUI" → Install → restart.
    • Or cd ComfyUI/custom_nodes && git clone https://github.com/da2el-ai/D2-nodes-ComfyUI and restart.

    Dependencies are just piexif and charset-normalizer, no model downloads. Pack gotcha: v32.0.0+ requires the newer ComfyUI V3 schema - older installs should pin a pre-32.0.0 release.

    Where people get burned

    Regexes are permissive by default. pony will match anywhere in the text, including inside a tag like ponytail - use anchors or word boundaries if that bites you. Also remember the -- split: if your output text legitimately contains --, it'll break the table. Keep outputs clean and put anything fancy in prefix/suffix. And if you're coming from D2 Regex Replace thinking this swaps text inside the prompt, it doesn't - this one selects whole output blocks, which is exactly what quality-tag switching needs.

    CategoryD2/Text

    Inputs (8)

    NameTypeDefaultDescription
    textSTRING
    regex_and_outputSTRINGpony -- score_9, -- -- highres, high quality,
    pre_delimCOMBO3 options: Comma, Line break, None
    suf_delimCOMBO3 options: Comma, Line break, None
    show_textCOMBO2 options: False, True
    text_checkSTRING
    prefixoptSTRING
    suffixoptSTRING

    Outputs (4)

    NameTypeDescription
    combined_textSTRING
    prefixSTRING
    suffixSTRING
    indexINT