Nodes/ComfyUI-Apt_Preset/math_text_compare
ComfyUI Node

math_text_compare

Turn a prompt string into a true/false switch

By cardenluo·Created 2 years ago·Updated 22 days ago· 309
math_text_compare
    • bool
    ignore_casetrue
    mode完全相等
    text_a
    text_b
    regex_pattern

    The name says "math" but there's no arithmetic here at all - this node compares two pieces of text and outputs a single true/false, which is exactly what you need to drive a switch or gate node elsewhere in a graph. Something like "if my prompt contains the word 'night', route through this ControlNet branch instead of that one" is the kind of logic this exists for. It just landed in Apt_Preset's data category alongside the actually-numeric math_Remap_data, which is a slightly odd shelf to put it on.

    Part of ComfyUI-Apt_Preset. No models, no downloads, pure string comparison.

    How it works

    mode picks the comparison. The node's own documentation lays out nine of them: exact equality, inequality, "A contains B", "A does not contain B", a check for whether the text is empty (blank strings and pure whitespace both count as empty), three length comparisons (equal, less than, greater than), and a regex match. ignore_case gets applied first, before whichever comparison runs, so "Night" and "night" are treated the same by default unless you turn it off.

    The regex mode is the flexible one and the one most likely to trip you up if you're not used to regex syntax. A bare pattern like black matches "black" anywhere in the string. Anchor it with ^black to require the match at the start, black$ to require it at the end, or ^black$ if you want the whole string to be exactly "black" and nothing else - that last one is the one people expect by default and don't get unless they add both anchors.

    Inputs and outputs

    • mode - which comparison to run. Default is exact equality.
    • ignore_case - fold case before comparing. On by default.
    • text_a - the primary string, multiline, required.
    • text_b (optional) - the second string, needed for every mode except the "is empty" check and regex match.
    • regex_pattern (optional) - only used when mode is set to the regex option; write your own anchors (^, $) for start/end/full matching.

    The single output is bool - wire it straight into whatever conditional or switch node in your graph branches on a boolean, whether that's stock ComfyUI logic nodes or another Apt_Preset utility.

    Installing it

    Through ComfyUI Manager: search "ComfyUI-Apt_Preset", install, restart. Manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/cardenluo/ComfyUI-Apt_Preset.git
    

    Restart ComfyUI. Windows users have install.bat in the repo for dependencies; Linux/Mac, install from the requirements file inside your ComfyUI venv the normal way. Nothing about this node needs an external file - it's string comparison, done in Python's standard library.

    Common issues

    Regex mode "matches" something you didn't expect. Remember an unanchored pattern matches anywhere in the string, not the whole thing. If you want a full match, write ^pattern$ yourself - the node doesn't add anchors for you, per its own description.

    "Text is empty" comes back false for a string that looks empty. Check for invisible characters - the node treats pure whitespace as empty (per its own documentation), but if there's a stray non-whitespace character in there (a zero-width space from a copy-paste, for instance) it won't register as blank.

    Case-sensitive comparison isn't working. Confirm ignore_case is actually off - it defaults to on, so "Prompt" and "prompt" will read as equal unless you explicitly flip it.

    Length comparison gives a surprising result. These modes compare raw character count, including whitespace and punctuation - a trailing space or newline in one of the two strings is enough to make "equal length" strings compare as unequal.

    CategoryApt_Preset/data

    Inputs (5)

    NameTypeDefaultDescription
    ignore_caseBOOLEANtrue
    modeCOMBO完全相等9 options: 完全相等, 不相等, A包含B, A不包含B, 文本是否为空, 长度A=长度B, +3
    text_aSTRING
    text_boptSTRING
    regex_patternoptSTRING

    Outputs (1)

    NameTypeDescription
    boolBOOLEAN