ComfyUI Node

substitute

Find-and-replace with a regex, the way your data actually needs

By StableLlama·Created about a year ago·Updated 4 days ago· 48
substitute
    • STRING
    string
    pattern
    repl
    count0

    Plain replace nodes fail you the moment the thing you want to swap is a shape instead of a literal string - every run of digits, any trailing whitespace, all the -/_/space variants of one separator. This node is Python's re.sub(), which replaces every match of a pattern with a replacement, and it's the most generally useful node in the pack's regex family precisely because it answers the question "how do I fix this string" for almost any answer.

    Real examples, from workflows that actually ship: strip numeric suffixes from a filename (re.sub(r"_\d+", "", name)), collapse multiple spaces (\s+ → single space), scrub a prompt of a model tag you no longer want, normalize a caption's separators. One node, and text cleanup becomes a config, not a chain of ten string operations.

    How it works

    re.sub(pattern, repl, string, count). A few specifics that matter:

    • count defaults to 0, meaning replace all occurrences. Set it to a positive number to limit to that many replacements - 0 is "all," which trips people used to replace's count semantics.
    • repl can use \1, \2 (or \g<name>) to reference capture groups from the pattern. Pattern (\d+)x(\d+), repl \1 by \2 turns "1920x1080" into "1920 by 1080". This is the power move.
    • A replacement string containing \ can surprise you - \1 in the replacement is a group reference, not a literal backslash. If you need a literal backslash in the output, escape it (\\).

    Inputs

    • string - the text to modify.
    • pattern - the regex to match.
    • repl - the replacement; supports \1/\g<name> group references.
    • count - max replacements, 0 = all (default 0).

    Output is the modified STRING.

    Installing it

    Ships in Basic data handling by StableLlama:

    cd ComfyUI/custom_nodes
    git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
    

    Restart, or install via ComfyUI Manager (search "Basic data handling"). The pack has zero dependencies and no model files, so this is the rare install that's genuinely safe to add.

    Gotchas

    • Greedy patterns replace more than you intended. .* will happily swallow to the end of the string.
    • If the pattern doesn't match, you get the original string back unchanged - no error, which is fine but means "did anything change?" needs an explicit check.
    • repl with $ characters is fine, but backslashes are where group references live. When your replacement is Windows-style path text, be careful - this is re.sub, and \ means something here.
    • Malformed regex = hard re.error failure. Valid but wrong = silent no-op. Same failure taxonomy as every node in this family.
    CategoryBasic/STRING/regex

    Inputs (4)

    NameTypeDefaultDescription
    stringSTRING
    patternSTRING
    replSTRING
    countINT0

    Outputs (1)

    NameTypeDescription
    STRINGSTRING