Nodes/comfyui-db0-pack/Regex Match To String
ComfyUI Node

Regex Match To String

Route your graph by what a string says

By db0·Created 6 months ago·Updated 6 months ago· 0
Regex Match To String
    • STRING
    string
    regex_pattern
    output_on_match
    output_on_non_match
    case_insensitivetrue
    multilinefalse
    dotallfalse

    The string version of "if this, then that"

    Regex Match To String is a conditional router for text. You give it a string, a regex pattern, and two possible outputs. If the pattern shows up in the string, it passes back output_on_match; otherwise it passes back output_on_non_match. There's no parsing, no extraction, no capture groups - it's a text-driven fork in the road.

    It's one of the two nodes in db0's personal pack (the other is Regex Match To Float, the numeric twin). Same author who runs AI Horde and made the Civitai Helper extension, but don't read anything grand into that - this is a two-node drawer of "for my own purposes" utilities, and it shows in a few opinionated defaults you should expect.

    When you'd reach for it

    Modern ComfyUI lives on text. Prompts, Danbooru tag lists, LLM/captioning node output, filenames - more of the decisions in a workflow live in strings than in numbers, yet the switch nodes everyone reaches for route on booleans and ints. This node is the bridge.

    Every string wire that forks is a candidate:

    • An LLM node returns different text → route it to the right branch downstream
    • Prompt contains a model's trigger word → send a different prompt/negative to the text encoder
    • Tag list contains score_9 or 1girl → pick which style prompt gets encoded
    • A filename from a loader matches a pattern → switch which path the graph continues down

    Because the output is itself a string, it composes straight into text encoders, prompt-concat nodes, and anything else that eats text.

    How it works

    One re.search() call from Python's stdlib, no dependencies. The three optional booleans become regex flags: case_insensitive (IGNORECASE), multiline (MULTILINE), and dotall - which, per the author's own tooltip, lets . match newline characters too. The result is a pure pass-through of whichever of the two string outputs you typed.

    The inputs that matter

    • string (multiline) - the text to test. Your prompt, tag list, LLM output, whatever.
    • regex_pattern (multiline) - the pattern to search for. Substring semantics again: fox matches the quick brown fox. Anchor with ^...$ if you need the whole string.
    • output_on_match / output_on_non_match (both multiline STRING) - the two texts it can forward. These are plain values, not templates; nothing from the match is substituted in.
    • case_insensitive - defaults to True, the opposite of most regex tools. If case matters to you, this is in the advanced section and you need to toggle it.

    Output: a single STRING, one of your two branch values. Feed it into a CLIP Text Encode, a text-concat node, a save-node path - anywhere text flows.

    Installing it

    ComfyUI Manager → Install Custom Nodes → search "comfyui-db0-pack" → install, restart. Manual route:

    cd ComfyUI/custom_nodes
    git clone https://github.com/db0/comfyui-db0-pack
    

    Restart ComfyUI. No extra pip packages, no model downloads - pure stdlib, so it's lightweight.

    Gotchas

    • Recent ComfyUI required. This pack uses the newer declarative comfy_api node API, not the classic decorator style. If the pack won't import, update ComfyUI - it won't load on an older install.
    • Case-insensitive by default is the thing most people forget. 1girl will match 1Girl, and if you assumed case mattered you'll spend a while chasing it.
    • re.search finds anywhere, not from the start. Pattern girl matches catgirl too. That's usually what you want for routing, but know it.
    • Malformed pattern = hard crash. No error handling around the regex call, so a syntax error in regex_pattern throws and aborts the run. Validate patterns in a scratch editor first.

    If all you need is "which of two text branches do I take," this is the node - tiny, dependency-free, and it makes the graph's intent readable at a glance.

    Categorydb0

    Inputs (7)

    NameTypeDefaultDescription
    stringSTRING
    regex_patternSTRING
    output_on_matchSTRING
    output_on_non_matchSTRING
    case_insensitiveoptBOOLEANtrue
    multilineoptBOOLEANfalse
    dotalloptBOOLEANfalseWhen enabled, the dot (.) character will match any character including newline characters. When disabled, dots won't match newlines.

    Outputs (1)

    NameTypeDescription
    STRINGSTRING