Nodes/comfyui-moonpack/Regex String Replace
ComfyUI Node

Regex String Replace

Regex find-and-replace with a test mode, so you don't nuke your prompt by accident

By moonwhaler·Created about a year ago·Updated 11 days ago· 16
Regex String Replace
    • text
    • match_count
    text
    pattern
    replacement
    case_insensitivefalse
    multilinefalse
    dotallfalse
    test_modefalse

    Regex is how you do find-and-replace when "find" isn't a literal string - when you want to remove every number, normalize - and _ variants, or collapse double spaces wherever they appear. Regex String Replace is MoonPack's full-power version of that, built on Python's re, with the flags you'll actually need and one feature that should be mandatory in every regex UI: a test mode.

    test_mode is the star. Flip it on and the node doesn't replace anything - it wraps every match in >>>...<<< and returns the highlighted text. You see exactly what your pattern matches before you let it loose on a real prompt. Regex that looks right on paper and matches nothing (or everything) is a rite of passage, and this node lets you do the rite of passage without destroying text.

    The inputs and outputs that matter

    • text - the source.
    • pattern - your Python regex. The pack even ships inline help with the common atoms (., \d, \w, [abc], (group)).
    • replacement - what matches become. Capture groups work the Python way: \1, \2, etc. An empty replacement deletes matches.
    • case_insensitive, multiline, dotall - toggles for re.IGNORECASE, re.MULTILINE (making ^/$ respect line boundaries), and re.DOTALL (making . match newlines). Multiline matters when you're processing multi-line text and your ^ unexpectedly anchors at the string start.
    • test_mode - highlight-only mode described above.
    • Outputs: text (the result - or the highlighted text in test mode) and match_count (INT), which tells you how many substitutions actually happened. Zero means your pattern matched nothing, which is its own diagnostic.

    Where it earns its keep

    Prompt cleanup is the obvious use: normalize whitespace, strip style tags you've stopped using, remove trailing commas before the encoder chokes on them. The less obvious one is data munging - extracting and reshaping text that came out of an API or a filename. Because the flags and the count are all on the node, it doubles as a debugging tool: turn on test mode, confirm the highlight, turn it off.

    One honest caveat: the multiline flag doesn't do what beginners hope. It changes what ^ and $ anchor to; it does not magically make .* span newlines. That's what dotall is for. They're separate toggles for separate problems, and having both on the node is genuinely useful.

    Installing it

    Part of MoonPack:

    cd ComfyUI/custom_nodes
    git clone https://github.com/moonwhaler/comfyui-moonpack.git
    

    or ComfyUI Manager → MoonPack → install, restart. Under MoonPack/string. No models, no third-party Python dependencies.

    Verdict

    If your string needs can be handled with literal find => replace lines, use Simple String Replace and stay sane. The moment you reach for character classes, alternation, or capture groups, this is the node - and test_mode plus match_count means you'll never ship a broken pattern blind again.

    CategoryMoonPack/string

    Inputs (7)

    NameTypeDefaultDescription
    textSTRINGSource text.
    patternSTRINGPython regex pattern.
    replacementSTRINGReplacement string. Use \1, \2, … for capture groups.
    case_insensitiveoptBOOLEANfalseApply re.IGNORECASE.
    multilineoptBOOLEANfalseApply re.MULTILINE (^/$ match line boundaries).
    dotalloptBOOLEANfalseApply re.DOTALL (. matches newlines).
    test_modeoptBOOLEANfalseHighlight matches with >>>…<<< instead of replacing.

    Outputs (2)

    NameTypeDescription
    textSTRING
    match_countINT