Nodes/ComfyUI Nich Utils/Select Text with Regular Expression (Nich)
ComfyUI Node

Select Text with Regular Expression (Nich)

Regex-extract part of any string mid-graph, without a code node

By nickve28·Created 2 years ago·Updated about a year ago· 9
Select Text with Regular Expression (Nich)
    • selected_text
    input
    regexp_filter
    delimiter

    Sometimes you don't want a whole prompt, you want part of it. Say you've got a long tag list and you only want the style tokens, or a filename you want to pull numbers out of, or a shared prompt template where you want to keep just the subjects. ComfyUI has no built-in "regex this string" node, and adding a full Python code node for one extraction is overkill. This tiny node is the shortcut: give it a string, a regular expression, and a delimiter, and it returns the matched pieces joined back together.

    It's the second node in the ComfyUI Nich Utils pack (nickve28), which its own README calls "still in early development! It will likely contain bugs or oddities." It's a utility, not a heavyweight - one function, no dependencies, no model files. For a tool this small that's mostly the point.

    How it works

    The whole mechanism is two Python calls: re.findall(regexp_filter, input), then delimiter.join(matches). The regex runs against the input string with Python's re engine (full pattern syntax - character classes, quantifiers, alternation, all of it). The default delimiter is a single space, so matching pieces come back as a space-joined string.

    Inputs: input (the text to search), regexp_filter (your pattern; it's a multiline field for readability), and delimiter (default " "). Output: selected_text - one string you can feed into a CLIP Text Encode, a filename, a metadata field, wherever a string goes.

    The regex gotchas that will actually trip you

    Because it uses findall, how your pattern treats capturing groups changes what comes back:

    • No groups: you get full matches. \b\d+\b on "v2 of 3" returns "2 3".
    • One capturing group: you get just the group, not the whole match. (red|blue) on "red car blue sky" returns "red blue" - which is usually what you wanted, but be aware a pattern like (\w+)! drops the !.
    • Two or more groups: findall returns tuples of captures, and the node's delimiter.join then crashes, because you can't join a tuple with a string. So (word) (word) is a hard error - avoid multiple capture groups, or wrap your whole pattern in one group plus non-capturing (?:...) for the rest.

    And the sneakiest one: leave regexp_filter blank and it compiles to a match-everything pattern, which findall turns into a string of empty matches - you get back whitespace garbage instead of the input. An empty regex is not "no filter"; it's a footgun. Always put an actual pattern in there.

    Install

    Via ComfyUI Manager, search "ComfyUI Nich Utils". Or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/nickve28/ComfyUI-Nich-Utils
    

    Restart ComfyUI. That's it - the node uses only Python's standard re library, so there are no extra dependencies to install and nothing to download.

    The honest take

    This is a thin node solving a narrow problem, and "select a subset of a prompt" is a genuinely narrow use case - you'll reach for it when you're building a reusable workflow with a structured prompt convention (tag prefixes, delimiters, version numbers) and you want downstream nodes to see only a slice. For anything more involved, a proper text-processing node pack will do more. But if your workflow already ships this pack for the Image from Dir Selector, this regex node is a free extra that costs nothing to keep in your back pocket - just keep your regex to a single capture group and never leave the filter empty.

    CategoryNich/utils

    Inputs (3)

    NameTypeDefaultDescription
    inputSTRING
    regexp_filterSTRING
    delimiterSTRING

    Outputs (1)

    NameTypeDescription
    selected_textSTRING