Nodes/geocine-comfyui/Text Replace
ComfyUI Node

Text Replace

The Search-and-Replace That Tells You It Worked

By geocine·Created 2 years ago·Updated 2 months ago· 1
Text Replace
    • result_text
    • replacement_count_number
    • replacement_count_float
    • replacement_count_int
    text
    find
    replace

    Every serious text pipeline ends up needing find-and-replace: swap a template token for an actual value, strip junk out of an LLM's answer, turn "masterpiece, best quality" into something your current checkpoint understands. Text Replace does that, and then does the part most such nodes skip - it tells you how many times the replacement actually happened.

    That detail is the reason to reach for this one over a DIY version. A replacement that silently matches nothing is a workflow bug wearing a disguise, and here it's a number you can see and even gate on.

    How it works

    It's one line of Python under the hood:

    modified_text, count = re.subn(find, replace, text)
    

    re.subn returns the transformed string and the number of substitutions. That count is what makes this node useful, because it lets you confirm the transformation occurred instead of trusting that it did.

    Inputs and outputs

    • text - the input string. It's forceInput, so it wants a wire, not a typed value.
    • find - what to look for. Here's the gotcha: this is a regular expression, not a plain substring. The code calls re.subn directly, no escaping. If your search string contains regex metacharacters - (, ), ., [, *, +, ? - they'll be interpreted, and your literal text will quietly never match. Looking for (masterpiece) literally? Escape it as \(masterpiece\). This is the single most common reason people think the node is broken.
    • replace - the replacement text.
    • result_text - the transformed string.
    • replacement_count_number / replacement_count_float / replacement_count_int - the same count, emitted in three type flavors so you can plug it into whatever numeric input your graph has. Zero means your find pattern didn't match, which is your early warning that a regex is wrong or a value didn't arrive.

    Where it fits

    The obvious use is prompt templating: build a prompt with {style} placeholders, then run the real value through Text Replace before the conditioner. It also shines after an LLM node - LLM output is full of markdown fences, stray preamble, and quoted cruft, and a couple of replace nodes will clean it into something the CLIP/text encoder actually likes. Wire the replacement_count_int into a comparison or a condition, and you can even branch: "if the token wasn't found, fall back to the default."

    Install

    Part of geocine-comfyui, one install gets you all eleven nodes:

    • ComfyUI Manager → search geocine-comfyui → install → restart
    • or Comfy CLI: comfy node install geocine-comfyui
    • or manually:
    cd ComfyUI/custom_nodes
    git clone https://github.com/geocine/geocine-comfyui
    

    then restart ComfyUI. No model downloads, no extra pip packages - the pack only needs openai, and only for the LLM node.

    Troubleshooting

    Replacement count is 0 but you're sure the text contains it. You've hit the regex trap above. Either escape the special characters or simplify your pattern. Replacement count is huge and the output is mangled. You probably wrote a regex with greedy wildcards like .* - it replaced far more than the token you meant. Test your pattern against a known input in any regex playground before wiring it in.

    Categorygeocine/text

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING
    findSTRING
    replaceSTRING

    Outputs (4)

    NameTypeDescription
    result_textSTRING
    replacement_count_numberNUMBER
    replacement_count_floatFLOAT
    replacement_count_intINT