提取前后字符🐠meeeyo.com
Grab everything before or after a marker — first or last occurrence, your call
- STRING
You know the workflow. Some node hands you a string that's really two things jammed together - a prompt with a LoRA name bolted on, a filename with a suffix, a config line with a value - and you need just one half of it. ExtractBeforeAfter (提取前后字符 in the UI) is the surgical answer: give it a pattern, tell it which side you want, and it returns everything before or after that marker. One input in, one STRING out. That's the whole node, and that's why it's useful.
How it works
The mechanism is exactly what find() and rfind() give you, wrapped in a menu. Four position choices, and they're self-explanatory once you see them translated:
- 保留最初之前 - keep what's before the first occurrence of the pattern
- 保留最初之后 - keep what's after the first occurrence
- 保留最后之前 - keep what's before the last occurrence
- 保留最后之后 - keep what's after the last occurrence
The "first vs last" distinction is the part people miss. If your pattern shows up twice and you don't think about it, you'll grab the wrong half and spend ten minutes wondering why. A string like id=7|name=cat|id=9 with pattern id= - "before first" gives you ``, "after last" gives you 9. Know which one you want.
The other input is include_delimiter, a boolean. Off (default) means the marker itself is cut out of the result; on means it stays in. For a filename like photo_001.jpg with pattern _, "after last" with include off gives you 001.jpg, with it on you get _001.jpg. Minor, but it saves a replace node if you care.
Why it beats the alternatives
ComfyUI's built-in text tools and the dozens of string packs out there mostly do replace or substring-by-length. ExtractBeforeAfter targets the specific, absurdly common case of "everything up to this character / everything after it" and makes it a dropdown instead of a regex. For prompt cleanup - say a prompt string has a <lora:xxx> chunk you want to keep only the clean part of - this is the fastest grab.
Install
Part of the ComfyUI_StringOps pack, so install that once:
cd ComfyUI/custom_nodes
git clone https://github.com/MeeeyoAI/ComfyUI_StringOps.git
or via ComfyUI Manager by searching "ComfyUI_StringOps". Restart after, no model downloads, nothing heavy in requirements. The pack's UI is Chinese-labeled (this one's 提取前后字符 with the 🐠meeeyo.com branding) - the four dropdown values are Chinese, so if you're not a Chinese reader, translate them once and pin the mapping above.
Common issues
Two practical gotchas. First, if the pattern isn't in the string at all, you get an empty string back - no error, no warning, just nothing. That's correct behavior but it's easy to mistake for a broken workflow; check the pattern spelling. Second, the whole node is case-sensitive and literal: photo won't match Photo. There's no regex mode here - if you need \d+-style extraction, this isn't the node; look at ExtractSpecificData from the same pack for the fancier rule-based stuff.
Simple, predictable, and it lands the common case. For a beginner it's one of the first StringOps nodes worth memorizing.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| input_string | STRING | — | |
| pattern | STRING | 标签符 | — |
| position | COMBO | 保留最初之前 | 4 options: 保留最初之前, 保留最初之后, 保留最后之前, 保留最后之后 |
| include_delimiter | BOOLEAN | false | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |