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

Extract text by regex

Pull one piece out of a string with a pattern

By yorkane·Created 2 years ago·Updated 6 months ago· 10
Extract text by regex
  • any1
  • any2
  • group_N
  • group_1
  • group_2
regex
group_number0
input_string

Where KY_RegexReplace rewrites text, this one just pulls a piece out. Point it at a string, give it a regex with a capture group, and it hands back the matched substring. Good for parsing a version number out of a filename, grabbing an ID out of a log line, or lifting one field out of a structured string somewhere in your graph without writing a custom script node.

How it works

You supply regex (the pattern) and group_number (which capture group you want, 0 meaning the whole match). It runs the pattern against input_string, and gives you back three outputs at once: group_N (whatever group you specified via group_number), plus group_1 and group_2 as fixed convenience outputs - so if your pattern has at least two capture groups, you get both of them without changing group_number and re-running the node twice.

Note that input_string is optional here (it's not required, unlike in KY_RegexReplace), and there are the same any1/any2 wildcard inputs seen across this pack's utility nodes - presumably an alternate way to feed in a non-string value that gets coerced before matching.

Installing it

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

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

Restart ComfyUI. Pure Python regex work, no extra dependencies, no model downloads. The pack's own README doesn't go beyond a features list - it's a small personal-toolbox pack with no real community footprint, so don't expect tutorials or a big install guide beyond the standard custom-node process.

Common issues

Remember that group 0 in Python's re is always the entire match, not the first parenthesized group - if group_number is 0 and you expected a specific captured piece, bump it to 1.

If your pattern has fewer capture groups than group_number asks for, or fewer than two groups total (which group_1/group_2 both assume exist), expect those outputs to come back empty rather than populated - write your pattern with enough groups for what you're pulling out of it.

If the regex doesn't match input_string at all, the sane expectation is an empty string on every output rather than an error, but always sanity-check by wiring the result into a preview/text node the first time you set up a new pattern - a silently-empty extraction is easy to miss downstream.

And keep in mind this node only ever does one pattern, one string, in one direction - it doesn't fall back to KY_RegexReplace's two-stage chaining or do any transformation of the extracted text. If you need to extract and then reformat, chain a KY_RegexReplace after it.

CategoryKYNode/Utils

Inputs (5)

NameTypeDefaultDescription
regexSTRING
group_numberINT00–100
any1opt*
any2opt*
input_stringoptSTRING

Outputs (3)

NameTypeDescription
group_NSTRING
group_1STRING
group_2STRING