Nodes/comfy-ovum/re.Match.expand (Regex Match Expand)
ComfyUI Node

re.Match.expand (Regex Match Expand)

Rebuild text from a match's groups with a template

By sfinktah·Created about a year ago·Updated 10 months ago· 7
re.Match.expand (Regex Match Expand)
  • match
  • expanded
template

match.expand(template) is the regex answer to "take what you matched and rebuild it into something else." OvumReMatchExpand takes a match object and a template, does backslash substitution on the template, and hands back the resulting string - \1 for the first group, \g<name> for named groups, \n and friends for escapes. Where re.Match.group pulls values out, expand reconstructs. That makes it perfect for renaming: capture the pieces of a filename, then reassemble them in a new order with new separators, all in one node.

How it works

Inputs are match (a RE_MATCH object, or a list of them) and template - the string with backreferences like \1 or \g<name>. For each match it runs m.expand(template) and returns the substituted string(s) via the expanded output. The escapes behave exactly like they do in re.sub's replacement string, so the mental model transfers: numeric backreferences, named backreferences, and \n-style character escapes all work.

Concrete example: a filename matched against (.+?)_(\d{5})\.png with a template of \2_\1.png turns batch_00042.png into 00042_batch.png. That's a rename done with one pattern and one template, no string surgery.

Why you'd reach for it

Renaming is the headline use. Reorder, re-separate, or re-prefix matched filenames deterministically. But it also shines for formatting: match a numeric group and re-emit it zero-padded, or build a normalized output string from several named groups so downstream nodes see a consistent shape regardless of input quirks. It pairs naturally with re.finditer - expand every match, and you've batch-transformed a whole list of entries.

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 models; light pure-Python deps installed by Manager.

The gotcha

The expanded output is typed LIST - even for a single match you get a list, so you may need an unwrap/index step before wiring into a plain STRING consumer. And the backslash rules: in the template, \1 is a group reference, so a literal backslash in your output needs \\. If your template has more backslashes than your input has groups, the extra references just come through literally - which is usually a sign your pattern and template aren't in sync. Verify group numbering with re.MatchView first.

Categoryovum/regex

Inputs (2)

NameTypeDefaultDescription
matchRE_MATCHThe match object (or list of match objects).
templateSTRINGThe template string for expansion, with backreferences like `\1` or `\g<name>`.

Outputs (1)

NameTypeDescription
expandedLIST