String Replacer
Find, replace, and count — regex optional
- result
- count
Cleaning up text is half the work of any automation: strip a model name out of a checkpoint path, normalize spaces, swap a tag separator, rewrite a prompt phrase before it hits the CLIP encoder. String Replacer does all of that in one node - with plain-string or regex matching, case-sensitivity control, and the bonus that it tells you how many replacements actually happened.
That count output is more useful than it sounds. It turns "did the replacement work?" from a visual check into a condition you can branch on.
How it works
You give it text, a search string, and a replace string. Plain mode does a direct substring replacement. Turn on use_regex and search is compiled as a regular expression with re.sub semantics, so groups and alternation work. Case sensitivity is a separate toggle - with it off, even plain-mode matching routes through a regex with re.IGNORECASE so the count stays accurate.
The count output is the number of replacements performed (re.subn's count), not a boolean. Zero means "nothing matched," which is your signal that something upstream changed.
Inputs
- text - the string to process (multiline allowed).
- search - what to find.
- replace - what to replace it with.
- use_regex - boolean, default off.
- case_sensitive - boolean, default on.
Outputs
- result - the modified string.
- count - how many substitutions happened.
Install
Standard pack install - ComfyUI Manager (search "ComfyUI-AutoFlow") or:
cd ComfyUI/custom_nodes
git clone https://github.com/ZXL-Xinram/ComfyUI-AutoFlow
Restart after. Needs only the stdlib re.
Where people get burned
- A bad regex returns the text unchanged. If the pattern fails to compile, the node logs the error and hands back the original text with
count = 0. No crash - which is friendly but means a typo'd regex silently does nothing. Watch the console. countvs. match. Acountof 0 is indistinguishable from "search found nothing" and "search didn't run." If you're branching on it, also branch on the text being non-empty.- Regex metacharacters in plain mode are literal. That's the point of the toggle - but it means searching for a literal
.requiresuse_regexoff, or\.on. Know which mode you're in. - Case-insensitive + regex is regex, so your search pattern still needs
\b,\detc. escaped correctly; the flag only changes case handling.
For prompt cleanup and filename normalization this is the most-workhorse node in the pack's string section. And the count output quietly gives your graph a way to know whether a transformation actually landed.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| search | STRING | — | |
| replace | STRING | — | |
| use_regex | BOOLEAN | false | Whether to use regular expressions |
| case_sensitive | BOOLEAN | true | Whether to be case sensitive |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| result | STRING | — |
| count | INT | — |