Replace Text with RegEx 🦬
A find-and-replace node that understands regex
- STRING
Replace Text with RegEx is a regex find-and-replace node - three text inputs, one string out. Simple enough to sound boring, powerful enough that once you start using it you'll wonder why you were hand-editing prompts in the first place.
It's part of Sokes Nodes 🦬, and it sits in the pack's grab-bag of text utilities that exist to massage strings before they hit the encoder.
How it works
It's a thin wrapper around Python's re.sub(regex_pattern, new, text) - literally one function call. Three inputs:
- text - the source string (multiline, so full prompts work).
- regex_pattern - the pattern to find.
- new - the replacement text.
Anything the pattern matches gets swapped for new, and the result comes out the single STRING output. Because it's real Python regex, you get the whole toolset: capture groups, character classes, anchors, lookarounds, the works. And new can reference capture groups - \1 refers to the first group, so text="img_01", regex_pattern="img_(\\d+)", new="render_\\1" gives you render_01.
The things people actually use it for
- Strip prompt weights:
(best quality:1.2)→best qualitywith pattern\(([^:]+):[\d.]+\)and replacement\1. - Clean up danbooru-style tag text before it hits a CLIP encoder that doesn't want underscores - replace
_with a space. - Normalize filenames: remove spaces, collapse repeated characters, enforce a naming convention.
- Scrub a workflow string before parsing, or clean up text that came back from an LLM/API node (which the pack's Runpod node produces).
Install
Standard Sokes Nodes 🦬 install:
cd ComfyUI/custom_nodes
git clone https://github.com/m-sokes/ComfyUI-Sokes-Nodes.git
pip install -r requirements.txt
or via ComfyUI Manager → search "ComfyUI Sokes Nodes" → Install, then restart.
Gotchas
Regex is where people fall over, and this node won't catch you:
- It's Python regex, not JavaScript. If you're copying patterns from a JS-based tool, some syntax (
\dworks, but certain flags or constructs differ) won't behave the same. Test on a small string first. - An invalid pattern raises an error that fails the node - no graceful fallback. A common culprit is an unmatched parenthesis or a stray backslash. If the node turns red, that's why.
- Watch your backslashes. As typed in the widget,
\dreaches Python regex fine (it's a valid digit escape). But a pattern that "matches nothing" is frequently a backslash problem - if you're trying to match a literal\, it has to be escaped, and character-class escapes you copy from elsewhere sometimes don't survive the round trip. Test on a short string first. newis a plain string, not a literal-find-replace for most tools' defaults - replacement happens on the full regex, so if you just want a simple swap, plain text without regex metacharacters works fine (e.g. replace "foo" with "bar").
Honestly, for a one-function node it punches well above its weight - any workflow that chains text transformations ends up wanting exactly this, and it's the kind of thing ComfyUI should ship but doesn't.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| regex_pattern | STRING | — | |
| new | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |