Nodes/ComfyUI/Extract Text
ComfyUI Node Runs on cloud

Extract Text

The regex scraper that turns one string into another

By Comfy-Org·Created 4 years ago·Updated about 3 hours ago· 129,870
Extract Text
    • STRING
    string
    regex_pattern
    mode
    case_insensitivetrue
    multilinefalse
    dotallfalse
    group_index1

    Extract Text (RegexExtract) is the scraping node: it pulls the part of a string that matches a regex pattern and hands it back as a new string. Where its sibling RegexReplace changes matched text, this one harvests it - pull the URL out of a caption, grab the number from an LLM's output, lift a tag out of a tag soup. It's the node that says "find me something shaped like this and give it to me."

    Think of the real use cases and it clicks immediately. Your Generate Text node spat out "Here is the prompt: a serene lake at dawn" - you want the part after the colon. An API response has a timestamp you need. A filename contains a seed number you want to feed the sampler. All of these are "find the pattern, keep the match" operations, and this node is the general-purpose version. It's also the honest way to parse LLM output that you asked to be structured but that came back with a sentence of preamble anyway.

    How it works

    The node has a mode dropdown that changes what it returns, and that's the real interface:

    • First Match - the whole first match, including everything the pattern captured (group 0). The "give me the URL" mode.
    • All Matches - every match, joined with newlines. The "list every number in this text" mode.
    • First Group - only the content of one capture group from the first match. This is the precision mode: id=(\d+) with group_index 1 returns just the digits, not the "id=" wrapper.
    • All Groups - group content from every match, joined with newlines.

    Supporting inputs: regex_pattern, plus the same flag trio as the rest of the regex family - case_insensitive (defaults to true), multiline, and dotall (off by default, so . doesn't cross newlines). And group_index, default 1, which picks which capture group the group modes return. Output is a single STRING - and on no match you get an empty string, not an error, which keeps downstream nodes alive.

    Common issues

    The first-timer trap is mode confusion: "First Match" returns the whole match including delimiters you probably didn't want, while "First Group" returns just the captured part - if your output has extra noise around the value, you're in the wrong mode, not the wrong pattern. Second: group_index counts from 1 (1 is the first capture group, 0 is the whole match) - the default of 1 is right for most patterns but wrong-feeling at first. Third: multiple matches come back newline-joined, so "All Matches" output has embedded line breaks - feed that into a one-line field and it'll look mangled. And because invalid regex returns empty rather than erroring, a typo in your pattern is silent; test the pattern somewhere visible before wiring it into automation.

    Categorytext

    Inputs (7)

    NameTypeDefaultDescription
    stringSTRING
    regex_patternSTRING
    modeCOMBO4 options: First Match, All Matches, First Group, All Groups
    case_insensitiveBOOLEANtrue
    multilineBOOLEANfalse
    dotallBOOLEANfalse
    group_indexINT10–100

    Outputs (1)

    NameTypeDescription
    STRINGSTRING