ComfyUI Node

Regex Substitution

Regex Substitution — find-and-replace, but with superpowers

By SeanScripts·Created 2 years ago·Updated 2 years ago· 79
Regex Substitution
    • STRING
    pattern
    string
    replace
    flagsM

    Regex Substitution is find-and-replace with a regex as the finder. Where a plain replace needs an exact string, this one matches a pattern, which means you can fix "the 3 cats and the 5 dogs" → "the 3 animals and the 5 animals", strip every number from a caption, or reformat an entire sentence in one pass. It's the most powerful of the pack's four regex nodes and the one most worth learning.

    Inputs: pattern (what to match), string (the text to edit), replace (what to substitute in), flags (default "M", the pack's letter flags). Output: the edited STRING.

    The trick that unlocks it: capture groups

    Put parts of the pattern in parentheses and reference them in the replacement with \1, \2, etc. Pattern (\d+) cats with replacement \1 animals turns "3 cats" into "3 animals" while leaving "the dogs" untouched. Without groups it's still useful - delete unwanted fragments by replacing a pattern with an empty string, or collapse messy whitespace with \s+" ". Python's re.sub does the heavy lifting; the flag letters map to the standard flags (i = ignore-case, m = multiline, s = dot-matches-newline).

    A note on alternatives

    The author points out this same node exists in Derfuu's ModdedNodes - a testament to how often people need it. If you already run Derfuu, you have a version; if you run this pack for the vision models, this one is already here. Either way, it's the same job.

    Install

    Ships with the SeanScripts pack - ComfyUI Manager search ComfyUI-PixtralLlamaVision, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/SeanScripts/ComfyUI-PixtralLlamaMolmoVision
    

    Restart after installing. No extra dependencies.

    Troubleshooting

    • Replacement shows \1 literally - the replace input needs the backslash escapes as typed; if an editor mangled them, re-enter as \1 not $1 (this is Python regex, where \1 is the group reference).
    • Nothing changed - the pattern didn't match. Test it in Regex Search first.
    • Replaced too much - greedy patterns like .* eat everything. Use .*? for the shortest match, or make the pattern more specific.

    It's the "stop hand-editing 400 captions" node. Learn the \1 trick and it'll do a frightening amount of cleanup for you.

    CategoryPixtralLlamaVision/Utility

    Inputs (4)

    NameTypeDefaultDescription
    patternSTRING
    stringSTRING
    replaceSTRING
    flagsSTRINGM

    Outputs (1)

    NameTypeDescription
    STRINGSTRING