Nodes/Suplex Misc ComfyUI Nodes/Text Regex Operations
ComfyUI Node

Text Regex Operations

Up to 20 chained regex find/replaces in a single node

By saftle·Created 2 years ago·Updated 10 months ago· 3
Text Regex Operations
    • STRING
    ◄text—►
    ◄num_operations1►
    ◄pattern_1—►
    ◄replacement_1—►
    ◄use_multiline_1true►
    ◄pattern_2—►
    ◄replacement_2—►
    ◄use_multiline_2true►
    ◄pattern_3—►
    ◄replacement_3—►
    ◄use_multiline_3true►
    ◄pattern_4—►
    ◄replacement_4—►
    ◄use_multiline_4true►
    ◄pattern_5—►
    ◄replacement_5—►
    ◄use_multiline_5true►
    ◄pattern_6—►
    ◄replacement_6—►
    ◄use_multiline_6true►
    ◄pattern_7—►
    ◄replacement_7—►
    ◄use_multiline_7true►
    ◄pattern_8—►
    ◄replacement_8—►
    ◄use_multiline_8true►
    ◄pattern_9—►
    ◄replacement_9—►
    ◄use_multiline_9true►
    ◄pattern_10—►
    ◄replacement_10—►
    ◄use_multiline_10true►
    ◄pattern_11—►
    ◄replacement_11—►
    ◄use_multiline_11true►
    ◄pattern_12—►
    ◄replacement_12—►
    ◄use_multiline_12true►
    ◄pattern_13—►
    ◄replacement_13—►
    ◄use_multiline_13true►
    ◄pattern_14—►
    ◄replacement_14—►
    ◄use_multiline_14true►
    ◄pattern_15—►
    ◄replacement_15—►
    ◄use_multiline_15true►
    ◄pattern_16—►
    ◄replacement_16—►
    ◄use_multiline_16true►
    ◄pattern_17—►
    ◄replacement_17—►
    ◄use_multiline_17true►
    ◄pattern_18—►
    ◄replacement_18—►
    ◄use_multiline_18true►
    ◄pattern_19—►
    ◄replacement_19—►
    ◄use_multiline_19true►
    ◄pattern_20—►
    ◄replacement_20—►
    ◄use_multiline_20true►

    Caption cleanup, prompt sanitizing, filename fixing - they all eventually turn into "apply these five regex substitutions, in order." Doing that with a chain of text nodes is miserable to build and harder to read. Text Regex Operations runs up to 20 find/replace operations in one node, each with its own pattern, replacement and multiline flag, and hands you the finished string. One node instead of a dozen, and you can see the whole pipeline at a glance.

    It's a re.sub pipeline with training wheels. Each operation's pattern runs against the text, its replacement is applied, and the result feeds the next operation. Standard Python regex semantics throughout, which means backreferences work (\1 and friends), character classes work, and if you know regex at all you know what this node does.

    How it works

    num_operations (1–20, default 1) says how many of the operations actually run. Each operation i has three inputs: pattern_i, replacement_i, and use_multiline_i (a flag that adds Python's re.MULTILINE). The node loops from 1 to num_operations and applies each in turn:

    processed_text = re.sub(pattern, replacement, processed_text, flags=flags)
    

    An invalid pattern doesn't crash the run - it prints a regex error to the console and moves on, leaving the text unchanged for that step. That's a friendlier failure mode than most text nodes offer.

    The inputs that matter

    • text - the string you're processing, multiline.
    • num_operations - how many of the 20 possible ops run.
    • pattern_N / replacement_N / use_multiline_N - per-op settings, present for N = 1 through 20. use_multiline defaults to true for every op.

    The README's example shows the shape of it. Input "*Start* of line and some extra text $5.99." with:

    • pattern ^\* → replacement - (bullet to dash)
    • pattern \*(.*?)\* → replacement `` (strip the asterisks)
    • pattern \$(.+) → replacement Price: \1 (label the price)

    ...produces "-Start of line and some extra text Price: 5.99.". That's a realistic caption-cleanup job in three ops.

    Where people get tripped up

    The use_multiline default of true matters more than it looks. With the flag on, ^ and $ match at every line boundary; with it off, they anchor to the whole string. For single-line prompts it makes no difference; for multiline text it's the difference between fixing every line and fixing only the first. The author's own example depends on it, so don't flip it off casually.

    Two more: operations run in order and each one sees the previous op's output - a later pattern can match text an earlier op just produced, so order is part of your design, not an afterthought. And an empty pattern means the op is skipped entirely, which is how you build a single saved template with fewer than 20 active ops.

    Install

    Same as every node in this pack: ComfyUI Manager → search "uber_comfy_nodes" ("Suplex Misc ComfyUI Nodes") → install → restart. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/saftle/uber_comfy_nodes
    

    Restart, find it under Uber Comfy. Light dependencies (Pillow, numpy, psutil, pynvml), no model downloads.

    CategoryUber Comfy

    Inputs (62)

    NameTypeDefaultDescription
    textSTRING—
    num_operationsINT11–20—
    pattern_1optSTRING—
    replacement_1optSTRING—
    use_multiline_1optBOOLEANtrue—
    pattern_2optSTRING—
    replacement_2optSTRING—
    use_multiline_2optBOOLEANtrue—
    pattern_3optSTRING—
    replacement_3optSTRING—
    use_multiline_3optBOOLEANtrue—
    pattern_4optSTRING—
    replacement_4optSTRING—
    use_multiline_4optBOOLEANtrue—
    pattern_5optSTRING—
    replacement_5optSTRING—
    use_multiline_5optBOOLEANtrue—
    pattern_6optSTRING—
    replacement_6optSTRING—
    use_multiline_6optBOOLEANtrue—
    pattern_7optSTRING—
    replacement_7optSTRING—
    use_multiline_7optBOOLEANtrue—
    pattern_8optSTRING—
    replacement_8optSTRING—
    use_multiline_8optBOOLEANtrue—
    pattern_9optSTRING—
    replacement_9optSTRING—
    use_multiline_9optBOOLEANtrue—
    pattern_10optSTRING—
    replacement_10optSTRING—
    use_multiline_10optBOOLEANtrue—
    pattern_11optSTRING—
    replacement_11optSTRING—
    use_multiline_11optBOOLEANtrue—
    pattern_12optSTRING—
    replacement_12optSTRING—
    use_multiline_12optBOOLEANtrue—
    pattern_13optSTRING—
    replacement_13optSTRING—
    use_multiline_13optBOOLEANtrue—
    pattern_14optSTRING—
    replacement_14optSTRING—
    use_multiline_14optBOOLEANtrue—
    pattern_15optSTRING—
    replacement_15optSTRING—
    use_multiline_15optBOOLEANtrue—
    pattern_16optSTRING—
    replacement_16optSTRING—
    use_multiline_16optBOOLEANtrue—
    pattern_17optSTRING—
    replacement_17optSTRING—
    use_multiline_17optBOOLEANtrue—
    pattern_18optSTRING—
    replacement_18optSTRING—
    use_multiline_18optBOOLEANtrue—
    pattern_19optSTRING—
    replacement_19optSTRING—
    use_multiline_19optBOOLEANtrue—
    pattern_20optSTRING—
    replacement_20optSTRING—
    use_multiline_20optBOOLEANtrue—

    Outputs (1)

    NameTypeDescription
    STRINGSTRING—