Nodes/comfy-ovum/re.escape (Regex Escape)
ComfyUI Node

re.escape (Regex Escape)

Stop regex metacharacters from hijacking your literal text

By sfinktah·Created about a year ago·Updated 10 months ago· 7
re.escape (Regex Escape)
    • escaped
    string
    string_in

    Every regex user hits this eventually: you want to search for a literal string - a filename, a path, a LoRA name like v2.1_realistic - and it fails, because the dots and parentheses and plus signs in your text are regex operators. OvumReEscape is re.escape in node form: it takes a plain string and backslashes every character that could change a regex's meaning, so you can safely drop user-generated text into a pattern. It's the difference between matching the actual filename and matching "any character, any digit, any character."

    How it works

    Feed it a string (or a list via string_in), and it returns the escaped version. Since Python 3.7, re.escape is conservative - it only escapes characters that genuinely alter regex meaning, so the output stays readable: v2.1_realistic becomes v2\.1_realistic. As of that version, only characters that can change the meaning of a regular expression are escaped; the noise of the old all-ASCII escaping is gone.

    Inputs:

    • string - the literal text to escape (used if the link isn't connected).
    • string_in - optional link; overrides the widget, accepts a string or list of strings.

    Output: escaped - the escaped string(s), list-shaped.

    Why you'd reach for it

    The classic pattern is building a search pattern from data you didn't write. Want to find a tag that came from a widget or a config file? Escape it first, then embed it in a larger pattern via a string-format node, then feed that to re.search. The same goes for paths and filenames - C:\output\batch(2) is full of regex operators, and without escaping, your search is matching something completely different than you intended.

    It also plays nicely with re.sub: escaping the search term turns a "replace this exact text everywhere" operation into a literal, safe replacement regardless of what punctuation the text contains.

    Installing it

    Ships in comfy-ovum. ComfyUI Manager → search "comfy-ovum", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/sfinktah/comfy-ovum
    

    Restart, find it under ovum/regex. No model downloads; light pure-Python deps installed by Manager.

    The gotcha

    Escape is for the search side of the pattern, not the replacement side. If you're putting a literal string into repl on a re.sub node, don't escape it - replacement strings treat \1 and friends specially, and escaping will double the backslashes into your output. And don't escape text you intend as a real pattern: \d+ is already a pattern, and escaping it turns it into a literal backslash-d-plus-plus. Escape only external, untrusted literals.

    Categoryovum/regex

    Inputs (2)

    NameTypeDefaultDescription
    stringSTRINGThe string with special regex characters to escape. Used if `string_in` is not connected.
    string_inoptSTRINGInput string or list of strings to escape. Overrides `string` widget if connected.

    Outputs (1)

    NameTypeDescription
    escapedSTRING