Nodes/GraftingRayman/GR Prompt Replacer
ComfyUI Node

GR Prompt Replacer

Find-and-replace for prompts, with a preview of exactly what changed

By GraftingRayman·Created 2 years ago·Updated about a month ago· 76
GR Prompt Replacer
    • text
    • highlighted_text
    text
    replacement_rulesblonde hair,black hair fair skin,tan skin blue eyes,brown eyes
    case_sensitivefalse
    match_whole_wordstrue
    sort_by_lengthtrue
    preserve_casetrue
    highlight_formatmarkdown

    This is sed for prompts. You write your prompt once, define a list of swap rules, and the node spits out the mutated version - no retyping the whole thing every time you want a variant with black hair instead of blonde, or tan skin instead of fair. The node's own default rules give away exactly what it was built for: quick physical-trait swaps across a batch of otherwise-identical prompts, the kind of thing you'd otherwise be hand-editing text boxes for.

    How it works

    You get two multiline text boxes: text (your actual prompt) and replacement_rules, where each line is one old,new pair. The default is a nice worked example - blonde hair,black hair, fair skin,tan skin, blue eyes,brown eyes - three independent swaps applied to the same prompt in one pass.

    A few toggles decide how literal-minded the matching is. case_sensitive (off by default) means "Blonde Hair" gets caught the same as "blonde hair." match_whole_words (on by default) stops it from replacing a fragment buried inside a longer word - useful, because plain string replace has no idea what a word boundary is otherwise. sort_by_length (on by default) matters more than it looks: if you have both a hair rule and a blonde hair rule, you want the longer phrase applied first, or the shorter rule eats part of it before the longer one ever gets a chance to match. preserve_case (on by default) tries to carry the capitalization pattern of what it replaced onto the replacement - so Blonde Hair becomes Black Hair, not a lowercase black hair stapled into a capitalized sentence.

    This node has no idea what your words mean - it's pure text substitution, not a language model doing context-aware editing. That's the tradeoff: fast and predictable, but it'll happily replace "blonde hair" inside "she dyed her blonde hair" and inside "blonde hairstyle magazine" identically, because to it those are the same six characters.

    The inputs and outputs that matter

    • text - your source prompt.
    • replacement_rules - one old,new swap per line.
    • case_sensitive, match_whole_words, sort_by_length, preserve_case - matching behavior, all sensible on their defaults.
    • highlight_format - markdown, html, or plain, controlling how the second output marks up what changed.

    Two outputs: text, the actual mutated prompt - wire this into your CLIP Text Encode. And highlighted_text, which marks up what got swapped in whichever format you picked - this one's for a text-preview node so you can eyeball the diff before you burn GPU time on it, not something you'd wire into a sampler.

    How to install it

    ComfyUI Manager, search GraftingRayman. Manual:

    cd ComfyUI/custom_nodes
    git clone https://github.com/GraftingRayman/ComfyUI_GraftingRayman
    

    Restart afterward. The whole pack needs OpenAI's CLIP installed separately or nothing imports at all - portable ComfyUI: .\python_embeded\python.exe -m pip install git+https://github.com/openai/CLIP.git; system Python: pip install git+https://github.com/openai/CLIP.git. This node doesn't touch CLIP itself, but the import failure takes the entire pack down with it, so you'll lose this node too if you skip the step.

    Common issues & troubleshooting

    Pack fails to load at all. Check your startup console for the CLIP import error and run the pip install above.

    A rule only partially applies, or applies somewhere you didn't expect. Almost always an overlapping-rule problem - check whether one of your rules is a substring of another and whether sort_by_length is doing what you expect. If you need substring replacement without word boundaries (matching tags glued together with no spaces, for instance), turn match_whole_words off.

    Capitalization looks wrong on the output. preserve_case tries to mirror the matched text's casing onto the replacement, but it's pattern-matching capitalization, not grammar - mixed-case or unusual capitalization in your source prompt can produce a replacement that looks a little off. Turn it off if you'd rather just get your replacement_rules values back verbatim.

    The replacement didn't happen and you don't know why. Read the highlighted_text output first - it'll show you exactly what the node thinks it matched (or didn't), which is faster than guessing at your rules from the final prompt alone.

    CategoryGR Utilities

    Inputs (7)

    NameTypeDefaultDescription
    textSTRING
    replacement_rulesSTRINGblonde hair,black hair fair skin,tan skin blue eyes,brown eyes
    case_sensitiveoptBOOLEANfalse
    match_whole_wordsoptBOOLEANtrueWhen enabled, 'man' won't match 'woman'
    sort_by_lengthoptBOOLEANtruePrevents shorter phrases from replacing parts of longer phrases
    preserve_caseoptBOOLEANtrueAttempt to maintain the case pattern of the original text
    highlight_formatoptCOMBOmarkdown3 options: markdown, html, plain

    Outputs (2)

    NameTypeDescription
    textSTRING
    highlighted_textSTRING