ComfyUI Node

Match Line

Find a Line in Any Text and Get a Number Back

By yolain·Created 3 months ago·Updated about 17 hours ago· 158
Match Line
    • LINE_INDEX
    text
    match

    The plumbing node for "which line said what?"

    Text in ComfyUI is usually a prompt. But a growing chunk of it is structured text - output from a node that emits several lines of data, a parsed list, a multi-track task dump. When you need to act on which line matched a pattern, you need this: easy matchLine scans a block of text line by line and returns the index of the first line that contains your match string. That's it. One number out, and the number is the whole point.

    Where it earns its keep: the Easy-Media MultiTrack Task Output node emits per-segment prompt data; an LLM node returns multi-line JSON-ish text; a subtitle parser hands back formatted lines. matchLine gives you an INT you can feed into a switch, a range selector, or a loop - "if the line mentioning 'sunset' is line 2, branch this way."

    How it works

    The mechanism is a one-liner's worth of Python:

    line_index = next(
        (index for index, line in enumerate(text.splitlines()) if match in line),
        -1,
    )
    

    Split the text on newlines, walk them in order, return the zero-based index of the first line where match is a substring. No regex, no case-folding - it's a plain in containment check. And two sentinel behaviors:

    • No match found → -1. The classic "not found" value, so your downstream switch can test for it.
    • Empty match-1 immediately. Matching against nothing is treated as no-match rather than "everything matches," which is the sane choice.

    Inputs and output

    • text - the multiline block to search.
    • match - the substring to look for. Single line, no multiline.
    • LINE_INDEX - an INT: the zero-based index of the first matching line, or -1.

    Setup and the small print

    It's part of Easy-Media - Manager → ComfyUI-Easy-Media, or git clone + restart. Pure logic, no models, no FFmpeg, nothing to download.

    Things that trip people:

    • It's substring matching, not equality. match = "cat" hits "concatenate," "category," and "cat" alike. If you need exact lines, pad your match with nothing - there's no anchor syntax here, so match on a distinctive substring instead.
    • Zero-based, not one-based. The first line is index 0. If you feed the result into a one-based consumer (many human-facing lists), you'll be off by one and it'll feel like a bug.
    • First match wins. If several lines contain the match, you get the earliest. There's no "which of many" - for that you'd loop or split yourself.
    • Whitespace matters. text.splitlines() keeps leading whitespace, so a match string with different indentation than the text won't hit. Match on a bare token, not the padded version.

    For the "read this structured text, decide what to do" pattern - which is half of any agentic or multi-segment workflow - this is the tiny node that turns text into a branch.

    CategoryEasyUse/Logic

    Inputs (2)

    NameTypeDefaultDescription
    textSTRING
    matchSTRING

    Outputs (1)

    NameTypeDescription
    LINE_INDEXINT