Minor updates including a custom replacement_delimiter

This commit is contained in:
BRADSEC
2025-01-25 19:15:30 +10:00
parent 470e2237f7
commit a35c2ddb34
7 changed files with 16 additions and 14 deletions
+6 -4
View File
@@ -2,8 +2,6 @@
Custom nodes for ComfyUI that provide text/string manipulation capabilities. Particularly useful for modifying text from LLM outputs (Ollama, Claude.ai, OpenAI ChatGPT), such as prompts or image-to-text description results.
![stringstrip](stringstrip.png)![stringreplace](stringreplace.png)
## Features
- Remove or replace text, words, numbers in input strings
- Handle multi-word strings (partial sentences/phrases)
@@ -11,7 +9,9 @@ Custom nodes for ComfyUI that provide text/string manipulation capabilities. Par
- Whole string matching option
## String Strip Node
Removes specified strings from input text. Each line should contain the string to be searched. The string can contain multiple words. To match individual words/strings place on seperate lines.
Removes specified strings from input text. Each line should contain the string to be searched. The string can contain multiple words. To match individual words/strings place on seperate lines.
![stringstrip](stringstrip.png)
### Inputs
- **`input_string`**: The string or text to process
@@ -24,11 +24,13 @@ Removes specified strings from input text. Each line should contain the string t
![examplestrip](examplestrip.png)
## String Replace Node
Replaces text strings with new strings. Each line should contain a search and replace pair separated by a colon `:` See example image below.
Replaces text strings with new strings. Each line should contain a search and replace pair separated replace_delimiter. See example image below.
![stringreplace](stringreplace.png)
### Inputs
- **`input_string`**: The string or text to process
- **`replacement_pairs`**: List of search:replace pairs (one per line)
- **`replacement_delimiter`**: Character(s) that separate search and replace strings. Default is a double colon `::`
- **`match_case`**: Enable/disable case-sensitive matching
- **`match_whole_string`**: Ensure only exact matches are replaced
- **`remove_extra_spaces`**: Clean up extra spaces after replacement
Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 96 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 83 KiB

+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "comfyui_stringessentials"
description = "Essential string manipulation nodes for ComfyUI."
version = "1.0.0"
version = "1.0.1"
license = "MIT"
[project.urls]
+9 -9
View File
@@ -6,10 +6,12 @@ class StringReplaceNode:
return {
"required": {
"replacement_pairs": ("STRING", {"multiline": True, "default": "",
"tooltip": "Enter replacement pairs, one per line, in format 'search:replace'. Example: 'cat:dog' or 'A drawing of:A photo of'"}),
"tooltip": "Enter replacement pairs, one per line. Format: 'search<delimiter>replace'. Default delimiter is ':' (colon)"}),
"replacement_delimiter": ("STRING", {"default": "::",
"tooltip": "Character(s) that separate search and replace strings. Default is two '::' (colons)"}),
"match_case": ("BOOLEAN", {"default": False,
"label_on": "enabled", "label_off": "disabled"}),
"match_whole_string": ("BOOLEAN", {"default": True,
"match_whole_string": ("BOOLEAN", {"default": False,
"label_on": "enabled", "label_off": "disabled"}),
"remove_extra_spaces": ("BOOLEAN", {"default": True,
"label_on": "enabled", "label_off": "disabled"})
@@ -21,16 +23,16 @@ class StringReplaceNode:
FUNCTION = "string_replace"
CATEGORY = "utils"
def string_replace(self, input_string, replacement_pairs, match_case, match_whole_string, remove_extra_spaces):
def string_replace(self, input_string, replacement_pairs, replacement_delimiter, match_case, match_whole_string, remove_extra_spaces):
result = input_string
flags = 0 if match_case else re.IGNORECASE
for line in replacement_pairs.splitlines():
line = line.strip()
if not line or ':' not in line:
if not line or replacement_delimiter not in line:
continue
search_str, replace_str = line.split(':', 1)
search_str, replace_str = line.split(replacement_delimiter, 1)
search_str = search_str.strip()
replace_str = replace_str.strip()
@@ -46,9 +48,7 @@ class StringReplaceNode:
result = re.sub(pattern, replace_str, result, flags=flags)
if remove_extra_spaces:
# Replace multiple spaces with single space
result = re.sub(r'\s+', ' ', result)
# Strip leading and trailing spaces
result = result.strip()
result = result.strip()
return (result,)
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 37 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 33 KiB