Nodes/ComfyCollectorNodes/String Replacer (CCN)
ComfyUI Node

String Replacer (CCN)

Batch find-and-replace for prompts, not just one swap

By valkymaera·Created 4 months ago·Updated 10 days ago· 1
String Replacer (CCN)
    • text
    • replacement_count
    text
    replacements
    case_sensitivetrue
    debugfalse

    ComfyUI's text tools are shockingly bad at "find this and replace it with that" once you need more than one swap. Core will let you write a string, but there's no native multi-rule find-and-replace. String Replacer (CCN) fixes that with one compact rule string: red,blue; cat,dog turns "the red cat sat on the red mat" into "the blue dog sat on the blue mat." Two swaps in one pass, driven by a single field.

    Where it earns its keep is prompt normalization. You've got a prompt bank with night city and sci-fi, and your model answers better to cityscape at night and cyberpunk. One rule string, wired between your prompt source and your encoder, and the whole library gets cleaned without touching the source. It's also the workhorse for fixing a common breed of prompt-tool output - swapping punctuation, normalizing a tag set, scrubbing a word that a negative embedding clashes with.

    How it works

    Rules are separated by ;, each rule is find,replace, and the split happens on the first comma only - so a replacement containing commas is fine. Rules apply sequentially: rule 2 sees the output of rule 1, which matters when rules overlap ("red,blue; blue,green" is not the same as the reverse). An empty replacement deletes the find text, which is how you strip things.

    Two settings control the matching:

    • case_sensitive (default on) - with it off, matching is literal but case-insensitive. It is not regex; the find text is escaped and treated as plain text. That's safer than regex, and it's the thing people forget when they try to use this like sed.
    • debug (default off) - prints per-rule replacement counts to the console, which is genuinely useful the first time a rule silently does nothing.

    The node also counts for you: replacement_count (INT) reports how many substitutions actually happened, so you can detect "this rule matched zero times" downstream instead of squinting at text.

    The inputs and outputs that matter

    • text - the source string (multiline).
    • replacements - the rule string: find,replace; find2,replace2.
    • case_sensitive, debug - both toggles, defaults on/off.

    Outputs: text (the result) and replacement_count (INT).

    How to install it

    Part of ComfyCollectorNodes - one install, every CCN node. ComfyUI Manager → search ComfyCollectorNodes → Install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/valkymaera/ComfyCollectorNodes.git
    

    Restart ComfyUI afterwards. The pack has no extra Python dependencies.

    Common issues

    The two things that burn people are both visible in the docs if you know to look. First, not regex - if you type \d+ expecting digits, you get a literal backslash-d. Second, sequential application - overlapping rules behave differently than a text editor's "replace all of both at once," because rule 2 runs on rule 1's output. If you find results that look half-applied, that's why.

    Also note rules that fail to parse (no comma) are silently skipped, with a console note only when debug is on. That's the node telling you "check your rule string," quietly.

    CategoryComfyCollectorNodes/String

    Inputs (4)

    NameTypeDefaultDescription
    textSTRING
    replacementsSTRING
    case_sensitiveBOOLEANtrue
    debugBOOLEANfalse

    Outputs (2)

    NameTypeDescription
    textSTRING
    replacement_countINT