Nodes/ComfyUI Sokes Nodes 🦬/Replace Text with RegEx 🦬
ComfyUI Node

Replace Text with RegEx 🦬

A find-and-replace node that understands regex

By m-sokes·Created 3 years ago·Updated 15 days ago· 4
Replace Text with RegEx 🦬
    • STRING
    ◄text—►
    ◄regex_pattern—►
    ◄new—►

    Replace Text with RegEx is a regex find-and-replace node - three text inputs, one string out. Simple enough to sound boring, powerful enough that once you start using it you'll wonder why you were hand-editing prompts in the first place.

    It's part of Sokes Nodes 🦬, and it sits in the pack's grab-bag of text utilities that exist to massage strings before they hit the encoder.

    How it works

    It's a thin wrapper around Python's re.sub(regex_pattern, new, text) - literally one function call. Three inputs:

    • text - the source string (multiline, so full prompts work).
    • regex_pattern - the pattern to find.
    • new - the replacement text.

    Anything the pattern matches gets swapped for new, and the result comes out the single STRING output. Because it's real Python regex, you get the whole toolset: capture groups, character classes, anchors, lookarounds, the works. And new can reference capture groups - \1 refers to the first group, so text="img_01", regex_pattern="img_(\\d+)", new="render_\\1" gives you render_01.

    The things people actually use it for

    • Strip prompt weights: (best quality:1.2) → best quality with pattern \(([^:]+):[\d.]+\) and replacement \1.
    • Clean up danbooru-style tag text before it hits a CLIP encoder that doesn't want underscores - replace _ with a space.
    • Normalize filenames: remove spaces, collapse repeated characters, enforce a naming convention.
    • Scrub a workflow string before parsing, or clean up text that came back from an LLM/API node (which the pack's Runpod node produces).

    Install

    Standard Sokes Nodes 🦬 install:

    cd ComfyUI/custom_nodes
    git clone https://github.com/m-sokes/ComfyUI-Sokes-Nodes.git
    pip install -r requirements.txt
    

    or via ComfyUI Manager → search "ComfyUI Sokes Nodes" → Install, then restart.

    Gotchas

    Regex is where people fall over, and this node won't catch you:

    • It's Python regex, not JavaScript. If you're copying patterns from a JS-based tool, some syntax (\d works, but certain flags or constructs differ) won't behave the same. Test on a small string first.
    • An invalid pattern raises an error that fails the node - no graceful fallback. A common culprit is an unmatched parenthesis or a stray backslash. If the node turns red, that's why.
    • Watch your backslashes. As typed in the widget, \d reaches Python regex fine (it's a valid digit escape). But a pattern that "matches nothing" is frequently a backslash problem - if you're trying to match a literal \, it has to be escaped, and character-class escapes you copy from elsewhere sometimes don't survive the round trip. Test on a short string first.
    • new is a plain string, not a literal-find-replace for most tools' defaults - replacement happens on the full regex, so if you just want a simple swap, plain text without regex metacharacters works fine (e.g. replace "foo" with "bar").

    Honestly, for a one-function node it punches well above its weight - any workflow that chains text transformations ends up wanting exactly this, and it's the kind of thing ComfyUI should ship but doesn't.

    CategorySokes 🦬

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING—
    regex_patternSTRING—
    newSTRING—

    Outputs (1)

    NameTypeDescription
    STRINGSTRING—