mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-06-03 10:02:09 +08:00
Add node for regex replace(sub) operation (#8340)
* Add node for regex replace(sub) operation * Apply suggestions from code review add tooltips Co-authored-by: Christian Byrne <abolkonsky.rem@gmail.com> * Fix indentation --------- Co-authored-by: Christian Byrne <abolkonsky.rem@gmail.com>
This commit is contained in:
parent
aeba0b3a26
commit
1d9fee79fd
@ -296,6 +296,41 @@ class RegexExtract():
|
|||||||
|
|
||||||
return result,
|
return result,
|
||||||
|
|
||||||
|
|
||||||
|
class RegexReplace():
|
||||||
|
DESCRIPTION = "Find and replace text using regex patterns."
|
||||||
|
@classmethod
|
||||||
|
def INPUT_TYPES(s):
|
||||||
|
return {
|
||||||
|
"required": {
|
||||||
|
"string": (IO.STRING, {"multiline": True}),
|
||||||
|
"regex_pattern": (IO.STRING, {"multiline": True}),
|
||||||
|
"replace": (IO.STRING, {"multiline": True}),
|
||||||
|
},
|
||||||
|
"optional": {
|
||||||
|
"case_insensitive": (IO.BOOLEAN, {"default": True}),
|
||||||
|
"multiline": (IO.BOOLEAN, {"default": False}),
|
||||||
|
"dotall": (IO.BOOLEAN, {"default": False, "tooltip": "When enabled, the dot (.) character will match any character including newline characters. When disabled, dots won't match newlines."}),
|
||||||
|
"count": (IO.INT, {"default": 0, "min": 0, "max": 100, "tooltip": "Maximum number of replacements to make. Set to 0 to replace all occurrences (default). Set to 1 to replace only the first match, 2 for the first two matches, etc."}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RETURN_TYPES = (IO.STRING,)
|
||||||
|
FUNCTION = "execute"
|
||||||
|
CATEGORY = "utils/string"
|
||||||
|
|
||||||
|
def execute(self, string, regex_pattern, replace, case_insensitive=True, multiline=False, dotall=False, count=0, **kwargs):
|
||||||
|
flags = 0
|
||||||
|
|
||||||
|
if case_insensitive:
|
||||||
|
flags |= re.IGNORECASE
|
||||||
|
if multiline:
|
||||||
|
flags |= re.MULTILINE
|
||||||
|
if dotall:
|
||||||
|
flags |= re.DOTALL
|
||||||
|
result = re.sub(regex_pattern, replace, string, count=count, flags=flags)
|
||||||
|
return result,
|
||||||
|
|
||||||
NODE_CLASS_MAPPINGS = {
|
NODE_CLASS_MAPPINGS = {
|
||||||
"StringConcatenate": StringConcatenate,
|
"StringConcatenate": StringConcatenate,
|
||||||
"StringSubstring": StringSubstring,
|
"StringSubstring": StringSubstring,
|
||||||
@ -306,7 +341,8 @@ NODE_CLASS_MAPPINGS = {
|
|||||||
"StringContains": StringContains,
|
"StringContains": StringContains,
|
||||||
"StringCompare": StringCompare,
|
"StringCompare": StringCompare,
|
||||||
"RegexMatch": RegexMatch,
|
"RegexMatch": RegexMatch,
|
||||||
"RegexExtract": RegexExtract
|
"RegexExtract": RegexExtract,
|
||||||
|
"RegexReplace": RegexReplace,
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||||
@ -319,5 +355,6 @@ NODE_DISPLAY_NAME_MAPPINGS = {
|
|||||||
"StringContains": "Contains",
|
"StringContains": "Contains",
|
||||||
"StringCompare": "Compare",
|
"StringCompare": "Compare",
|
||||||
"RegexMatch": "Regex Match",
|
"RegexMatch": "Regex Match",
|
||||||
"RegexExtract": "Regex Extract"
|
"RegexExtract": "Regex Extract",
|
||||||
|
"RegexReplace": "Regex Replace",
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user