Nodes/ComfyUI-KYNode/Replace text by regex
ComfyUI Node

Replace text by regex

Chained find-and-replace for prompts and paths

By yorkane·Created 2 years ago·Updated 6 months ago· 10
Replace text by regex
  • any1
  • any2
  • regex1-result
  • final-result
input_string
regex1
replace1
regex2
replace2

This is the node you reach for when you're stripping trigger words out of a caption, cleaning up a filename before it hits a save node, or reformatting text mid-graph without writing a whole Python node to do it. It runs one regex-and-replace pass on a string, and optionally a second pass chained right after the first.

Small honest aside: the node's own internal docstring, as shipped in the pack, literally reads "Converts any type to a string" - clearly a copy-paste leftover from a neighboring utility node in the same file. Ignore it. What it does, per its name and its actual inputs, is regex replace.

How it works

Feed input_string and a pattern/replacement pair (regex1/replace1) and it runs a standard find-and-replace, giving you regex1-result. If you also fill in regex2/replace2, that second pattern runs against the first result and you get final-result - a two-stage pipeline in one node instead of chaining two separate ones. If you leave the second pair blank, final-result should just mirror regex1-result.

There are also two wildcard any1/any2 inputs typed as * - they'll accept a value of any type from elsewhere in your graph. Given that leftover "converts any type to a string" docstring, the likely intent is that these let you feed in something non-string (a number, a boolean) and have it coerced before the regex runs, instead of forcing you to insert a manual string-cast node first.

Installing it

ComfyUI Manager: search ComfyUI-KYNode, install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/yorkane/ComfyUI-KYNode

Restart ComfyUI. No dependencies beyond what ComfyUI already ships - this is pure Python string/regex work, no external binaries or model downloads. Fair warning on the pack overall: the README is a bare feature list with no install or requirements section, and it's a niche personal-toolbox pack with basically no presence in the wider ComfyUI community - you're not missing some famous tool here, just a handy grab-bag.

Common issues

Regex syntax is Python's re module, not a simplified pattern language - if you're used to typing a literal backslash for things like \d or \s in a plain text field, remember ComfyUI text widgets pass the characters through as-is, so a single backslash is usually what you want (not \\d), but double-check if your pattern isn't matching.

If replace1 uses backreferences like \1 to reuse a captured group from regex1, that follows the standard re.sub convention - but this node's job is straight replace, not extraction, so if you actually need to pull a captured group out on its own (rather than reinsert it into a replacement), that's what KY_RegexExtractor is for.

An empty regex1 should just pass the string through unchanged rather than erroring, which is a reasonable way to temporarily disable a stage without deleting the node. And since final-result depends on regex2/replace2 being filled, don't be surprised if it's identical to regex1-result - that's the expected behavior when you're only using one pass, not a bug.

CategoryKYNode/Utils

Inputs (7)

NameTypeDefaultDescription
input_stringSTRING
regex1STRING
replace1STRING
any1opt*
any2opt*
regex2optSTRING
replace2optSTRING

Outputs (2)

NameTypeDescription
regex1-resultSTRING
final-resultSTRING