Nodes/ComfyUiNodes/M Regex Replace (picked option)
ComfyUI Node

M Regex Replace (picked option)

The regex replacer that also rolls the dice for you

By Madlumi·Created 8 months ago·Updated 8 months ago· 0
M Regex Replace (picked option)
    • next_seed
    • out_text
    • picked_option
    • picked_index
    n0
    text
    patternSKIN
    optionspink green blue
    case_insensitivefalse
    multilinefalse
    dotalltrue
    max_replacements0

    "M Regex Replace (picked option)" is two nodes in one trench coat. Give it a text string, a regex pattern, a list of replacement options, and a number n, and it does what the long display name promises: it picks one option from the list based on n, then regex-replaces matches in your text with that pick. It's the seed-driven cousin of wildcard syntax.

    The wildcard way - a young woman with {brown|blonde|black} hair - picks a random alternative per run and is genuinely handy for batching character reference sets. This node is the deterministic version of the same trick. The choice isn't random: it's a pure function of the number sitting in n. Same n, same list, same pick, forever. That reproducibility is the whole point - you can hold a seed, vary everything else, and still know exactly which option was active when you got that image.

    How the pick works

    The mechanism is small and easy to trust once you see it. options is the one multiline widget on the node, one choice per line (blank lines are dropped). The n input gets run through splitmix64, a tiny 64-bit hash, and the hash modulo the number of options gives you picked_index and picked_option. Meanwhile a masked copy of the hash comes out as next_seed - wire that back into n and you step to the next different option on every run, walking deterministically through the whole list instead of rolling from scratch.

    Two nice touches. First, the input is deliberately named n, not seed - the source comments say exactly why: ComfyUI would otherwise slap a "control after generate" dropdown on it, the widget that silently overwrites your good seed at the end of each run. Naming it n sidesteps that classic injury entirely. Second, the replacement itself is plain re.sub with Python's re module and the flags you toggle.

    The inputs that matter

    • n - the picker seed. Change it to get a different option.
    • pattern - a Python regex. The default "SKIN" is just the author's toy example; replace it with whatever you're matching.
    • options - the list to pick from, one per line. Only multiline field on the node.
    • text - the string being edited.
    • case_insensitive, multiline, dotall - regex flags. dotall defaults to true, which is unusual: . matches newlines, so if you expect the Python default, this catches you out. Flip it off when your pattern relies on . not crossing lines.
    • max_replacements - cap how many matches get swapped; 0 means all of them.

    The outputs

    out_text is what you wire into a CLIP Text Encode or any string socket. picked_option and picked_index are handy for labels, filename metadata, or downstream logic. next_seed goes back into n to chain picks.

    Installing it

    It ships in the small ComfyUiNodes pack. Easiest via ComfyUI Manager - search "ComfyUiNodes" and hit install - or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Madlumi/ComfyUiNodes
    # restart ComfyUI
    

    There's no requirements.txt and no model download; the whole pack is Python stdlib. In an ecosystem where dependency hell is the number one complaint, that's as clean an install as you'll find.

    Gotchas worth knowing

    • The replacement-string trap. The picked option is used as re.sub's replacement template, so a \1, \g<name>, or stray backslash inside an option is interpreted as a group reference - and a bad escape can error mid-run. Keep options plain text; avoid backslashes and regex group syntax.
    • Empty options return your text untouched, an empty picked_option, and index 0. No error, just a no-op.
    • The pick is a hash of n, so reordering the options list changes which option a given n selects. Don't be surprised when the same n picks a different line after you edit the list.
    • If "nothing is being replaced," check the pattern actually matches - with dotall on by default, that usually means the pattern matches more than you wanted, not less.
    Categorymnodes/string

    Inputs (8)

    NameTypeDefaultDescription
    nINT00–9223372036854776000
    textSTRING
    patternSTRINGSKIN
    optionsSTRINGpink green blue
    case_insensitiveBOOLEANfalse
    multilineBOOLEANfalse
    dotallBOOLEANtrue
    max_replacementsINT00–999999

    Outputs (4)

    NameTypeDescription
    next_seedINT
    out_textSTRING
    picked_optionSTRING
    picked_indexINT