From 58c7589b41dd26d3d802da567e1373fae512a76a Mon Sep 17 00:00:00 2001 From: huang zhe Date: Wed, 12 Feb 2025 11:31:36 +0800 Subject: [PATCH] written by cursor update readme by cursor cursor written: add readme by translation --- README.md | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++- README_CN.md | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ __init__.py | 3 ++ regex_node.py | 54 ++++++++++++++++++++++++++++ 4 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 README_CN.md create mode 100644 __init__.py create mode 100644 regex_node.py diff --git a/README.md b/README.md index d436587..dd03c8d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,98 @@ # ComfyUI-Regex-Runner -help to run regex in comfyui + +A ComfyUI custom node plugin that allows you to run regular expression operations directly in ComfyUI workflows. + +[中文文档](README_CN.md) + +## Features + +- Execute regex matching and replacement in ComfyUI +- Support all standard Python regular expression syntax +- Easy text input and output handling +- Fully integrated into ComfyUI workflows + +## Installation + +1. Navigate to your ComfyUI custom nodes directory: + +```bash +cd ComfyUI/custom_nodes/ +``` + +2. Clone this repository: + +```bash +git clone https://github.com/ltdrdata/ComfyUI-Regex-Runner +``` + +## Usage + +### Available Nodes + +The plugin provides the following nodes: + +- **Regex Match**: Performs regular expression matching operations +- **Regex Replace**: Performs regular expression replacement operations + +### Node Parameters + +#### Regex Match Node +- `text`: Input text to match +- `pattern`: Regular expression pattern +- `flags`: Regex flags (optional) +- Output: List of matched texts + +#### Regex Replace Node +- `text`: Input text to process +- `pattern`: Regular expression pattern +- `replacement`: Replacement text +- `flags`: Regex flags (optional) +- Output: Replaced text + +### Examples + +1. **Extract Numbers** +```python +Input text: "Image size is 512x512" +Pattern: "\d+" +Output: ["512", "512"] +``` + +2. **Replace Text** +```python +Input text: "prompt: a cat" +Pattern: "^prompt: " +Replacement: "negative prompt: " +Output: "negative prompt: a cat" +``` + +## Common Issues + +1. **Regex Not Matching?** + - Check if the regex syntax is correct + - Verify if you need to use raw strings (r"pattern") + - Ensure special characters are properly escaped + +2. **How to Use Regex Flags?** + - Supports standard Python regex flags + - Examples: `re.IGNORECASE`, `re.MULTILINE`, etc. + +## Contributing + +Contributions via Pull Requests are welcome. Please ensure: + +1. Code follows Python coding standards +2. Appropriate test cases are provided +3. Documentation is updated + +## License + +MIT License + +## Support + +If you encounter any issues or have suggestions for improvements: + +1. Submit an issue on GitHub +2. Provide detailed problem description and steps to reproduce +3. Include relevant error messages and logs diff --git a/README_CN.md b/README_CN.md new file mode 100644 index 0000000..6faf832 --- /dev/null +++ b/README_CN.md @@ -0,0 +1,96 @@ +# ComfyUI-Regex-Runner + +ComfyUI-Regex-Runner 是一个 ComfyUI 的自定义节点插件,它允许你在 ComfyUI 工作流中直接运行正则表达式操作。 + +## 功能特点 + +- 在 ComfyUI 中执行正则表达式匹配和替换 +- 支持所有标准的 Python 正则表达式语法 +- 可以轻松处理文本输入和输出 +- 完全集成到 ComfyUI 工作流程中 + +## 安装方法 + +1. 进入你的 ComfyUI 自定义节点目录: + +```bash +cd ComfyUI/custom_nodes/ +``` + +2. 克隆此仓库: + +```bash +git clone https://github.com/ltdrdata/ComfyUI-Regex-Runner +``` + +## 使用方法 + +### 基本节点介绍 + +插件提供以下节点: + +- **Regex Match**: 执行正则表达式匹配操作 +- **Regex Replace**: 执行正则表达式替换操作 + +### 节点参数说明 + +#### Regex Match 节点 +- `text`: 输入要匹配的文本 +- `pattern`: 正则表达式模式 +- `flags`: 正则表达式标志(可选) +- 输出:匹配到的文本列表 + +#### Regex Replace 节点 +- `text`: 输入要处理的文本 +- `pattern`: 正则表达式模式 +- `replacement`: 替换文本 +- `flags`: 正则表达式标志(可选) +- 输出:替换后的文本 + +### 使用示例 + +1. **提取数字** +```python +输入文本: "图片尺寸为 512x512" +正则模式: "\d+" +输出: ["512", "512"] +``` + +2. **替换文本** +```python +输入文本: "prompt: a cat" +正则模式: "^prompt: " +替换文本: "negative prompt: " +输出: "negative prompt: a cat" +``` + +## 常见问题 + +1. **正则表达式不匹配?** + - 检查正则表达式语法是否正确 + - 确认是否需要使用原始字符串(r"pattern") + - 验证特殊字符是否正确转义 + +2. **如何使用正则表达式标志?** + - 支持标准 Python 正则表达式标志 + - 例如:`re.IGNORECASE`, `re.MULTILINE` 等 + +## 贡献指南 + +欢迎提交 Pull Requests 来改进这个项目。请确保: + +1. 代码符合 Python 编码规范 +2. 提供适当的测试用例 +3. 更新相关文档 + +## 许可证 + +MIT License + +## 问题反馈 + +如果你遇到任何问题或有改进建议,请: + +1. 在 GitHub 上提交 Issue +2. 提供详细的问题描述和复现步骤 +3. 附上相关的错误信息和日志 \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..4e36d80 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .regex_node import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] \ No newline at end of file diff --git a/regex_node.py b/regex_node.py new file mode 100644 index 0000000..0295f8d --- /dev/null +++ b/regex_node.py @@ -0,0 +1,54 @@ +import re + +class RegexNode: + """用于执行正则表达式操作的节点""" + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "input_text": ("STRING", {"default": "", "multiline": True}), + "pattern": ("STRING", {"default": ""}), + "replace_with": ("STRING", {"default": ""}), + }, + "optional": { + "mode": (["search", "replace", "findall"], {"default": "search"}), + } + } + + RETURN_TYPES = ("STRING",) + FUNCTION = "execute_regex" + CATEGORY = "text" + + def execute_regex(self, input_text, pattern, replace_with="", mode="search"): + try: + if mode == "search": + match = re.search(pattern, input_text) + return (match.group(0) if match else "",) + + elif mode == "replace": + result = re.sub(pattern, replace_with, input_text) + return (result,) + + elif mode == "findall": + matches = re.findall(pattern, input_text) + return ("\n".join(matches) if matches else "",) + + except re.error as e: + print(f"正则表达式错误: {str(e)}") + return ("",) + except Exception as e: + print(f"执行错误: {str(e)}") + return ("",) + +# 注册节点 +NODE_CLASS_MAPPINGS = { + "RegexNode": RegexNode +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "RegexNode": "Regex Operations" +} \ No newline at end of file