Nodes/comfy-ovum/re.subn (Regex SubN)
ComfyUI Node

re.subn (Regex SubN)

The same find-and-replace as re.sub, but the count is the point

By sfinktah·Created about a year ago·Updated 10 months ago· 7
re.subn (Regex SubN)
  • flags
  • result
  • count
pattern
string
repl
string_in
count0

Honest take up front: in this pack, re.subn and re.sub are near-identical. Python distinguishes them - sub returns just the new string, subn returns (new_string, number_of_subs) - but the pack's OvumReSub already hands you a count output alongside result. So OvumReSubN isn't a different capability; it's the same substitution engine with the same two outputs. Which one you pick is basically preference. I reach for re.sub because it's the name everyone knows, but if you're following a Python cheat-sheet that says "use subn when you need the count," this node matches that mental model exactly.

How it works

Identical mechanics to OvumReSub: compile pattern, replace occurrences with repl in each input string, honor count (0 = all) and flags. repl supports the full backreference set - \1, \g<name>, \n. The outputs are result (the edited string, list-shaped) and count (number of substitutions, list-shaped). If nothing matches, result passes through unchanged and count is 0.

Inputs:

  • pattern - multiline regex.
  • repl - the replacement, with backslash escapes and backreferences.
  • string - text to edit.
  • string_in - optional; overrides string, accepts a list.
  • flags - RE_FLAGS from re.compile flags (default IGNORECASE on).
  • count - max replacements, default 0 (all).

When the count actually matters

The count output is more useful than it looks. Wiring it into a conditional gives you a free "did anything change" signal: count == 0 means the pattern never appeared, which you can branch on. That's genuinely handy for conditional cleanup - normalize a string only if it contained the pattern - and it's the honest reason this node exists alongside re.sub. For everything else, treat it as the twin of OvumReSub.

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

Don't go hunting for a difference that isn't there. If a tutorial workflow shows re.subn and you build it with re.sub, nothing breaks - the outputs are the same shape. Pick one, wire result and count the same way, and move on. The only real trap is forgetting that repl's backslashes are processed, so a literal \1 in your output needs escaping.

Categoryovum/regex

Inputs (6)

NameTypeDefaultDescription
patternSTRINGThe regular expression pattern.
stringSTRINGThe string to search. Used if `string_in` is not connected.
replSTRINGThe replacement string or pattern.
string_inoptSTRINGInput string or list of strings. Overrides `string` widget if connected.
flagsoptRE_FLAGS2Regex compilation flags.
countoptINT0Maximum number of pattern occurrences to be replaced. 0 means replace all.

Outputs (2)

NameTypeDescription
resultSTRING
countINT