Regex Extract (NH)
Pull, replace, or split text without leaving ComfyUI
- result
- matched
- groups
- count
Regex is the Swiss-army knife of text processing, and Regex Extract (NH) is that knife bolted into a ComfyUI node. You feed it a text and a pattern, pick a mode, and get structured results back - no external script, no Python node, no leaving the graph.
The mode dropdown decides what happens, and the four options map directly onto Python's re module:
- match - finds the first match and returns it in
result, with the capture groups ingroups(one per line).matchedis True if anything was found. - findall - finds every match and joins them into
result, one per line. This is the one you want for extracting every email, every number, every filename from a blob of text. - replace - swaps every match for your
replacementstring viare.sub, withcountreporting how many replacements happened. - split - splits the text on the pattern and returns the parts, one per line, dropping empties.
The outputs are consistent across modes: result (STRING), matched (BOOLEAN), groups (STRING), and count (INT). That last one is quietly useful - count gives you a cheap way to branch: "did we find more than one match?" feeds a comparison node and lets the graph decide what to do.
The beginner traps
The default pattern is (\d+) - every run of digits - which is a reasonable starting point but also the thing that will bite you if you forget to change it. Two specifics worth knowing: match returns the first match only, while findall gets all of them; and with capture groups, findall can return tuples, which the node flattens into comma-joined lines rather than dropping data. A malformed pattern (bad escaping, unclosed group) doesn't crash the run - the node catches the error, logs it to the console, and passes your text through unchanged with matched False. So if everything silently "works" but nothing extracts, the pattern is your first suspect.
One honest limitation: this is single-text, single-pattern. There's no multi-pattern alternation UI, no regex builder, no "test here" preview panel. For one-off extraction in a workflow it's great; if you're debugging a gnarly pattern, build it in a regex tester first and paste it in.
Install
It's part of NH-Nodes:
- ComfyUI Manager → search NH-Nodes → install → restart ComfyUI. Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/NH-Nodes.git
cd NH-Nodes
pip install -r requirements.txt
Then restart ComfyUI.
No models, no extra deps.
Common issues
The most common failure is a pattern that looks right but needs escaping - regex backslashes in ComfyUI strings can get mangled, so if \d behaves oddly, check whether you've written \\d. And remember matched is your friend: it's the cleanest way to detect "nothing found" without parsing the text yourself. If count and result disagree, you're probably in replace mode, where count counts substitutions on the original text - which is exactly what you want to know.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| pattern | STRING | (\d+) | — |
| mode | COMBO | 4 options: match, findall, replace, split | |
| replacementopt | STRING | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| result | STRING | — |
| matched | BOOLEAN | — |
| groups | STRING | — |
| count | INT | — |