You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

拆分字符串不切割关键词:如何以[Chapter]拆分含方括号字符串

Hey there! Let's break down how to solve your two string splitting needs with clear, actionable examples.

需求1:拆分字符串时确保不切割关键词

When you need to split a string but don't want to split through specific keywords, the core idea is to only split at positions that don't fall within or adjacent to your protected keywords. Here are two practical approaches using Python:

思路1:正则表达式匹配安全拆分点

Use regex lookarounds to avoid splitting points that are part of or connected to your protected keywords. This works well for more complex scenarios where keywords might appear in different contexts.

import re

def split_without_cutting_keywords(input_str, keywords, split_char=" "):
    # Escape keywords to handle special regex characters like brackets or dots
    escaped_keywords = [re.escape(kw) for kw in keywords]
    # Build a pattern that matches split characters NOT inside or next to keywords
    pattern = rf"(?<!{'|'.join(escaped_keywords)}){re.escape(split_char)}(?!{'|'.join(escaped_keywords)})"
    
    # Split the string using the safe pattern
    return re.split(pattern, input_str)

# Test case
input_text = "foo bar hello world baz test hello world again"
protected_keywords = ["hello world"]
result = split_without_cutting_keywords(input_text, protected_keywords)
print(result)
# Output: ['foo', 'bar', 'hello world', 'baz', 'test', 'hello world', 'again']

思路2:临时占位符替换

For simpler cases, replace your keywords with a unique placeholder first, split the string normally, then restore the keywords. This is easy to read and implement.

def split_with_placeholder(input_str, keywords, split_char=" "):
    placeholder = "###PROTECTED_KEYWORD###"
    temp_str = input_str
    
    # Replace all protected keywords with the placeholder
    for kw in keywords:
        temp_str = temp_str.replace(kw, placeholder)
    
    # Split the temporary string
    split_parts = temp_str.split(split_char)
    
    # Restore the original keywords in each part
    return [part.replace(placeholder, kw) if placeholder in part else part for part in split_parts]

# Test case
result = split_with_placeholder(input_text, protected_keywords)
print(result)
# Same output as above
需求2:按"[Chapter"拆分并获取前两部分

This one is straightforward—you just need to split the string only once at the first occurrence of "[Chapter", then grab the first two elements from the result. Note that the split operation removes the "[Chapter" substring from the second part; if you want to retain it, you'll need to prepend it back manually.

Example Code (Python)

def split_at_chapter(input_str):
    # Split once at "[Chapter" — the "1" ensures we only split the first occurrence
    parts = input_str.split("[Chapter", 1)
    
    # Handle edge case where "[Chapter" doesn't exist in the string
    if len(parts) == 1:
        return (parts[0], "")  # Return empty string as second part if no split found
    
    # If you don't need to keep "[Chapter" in the second part, return as-is
    return (parts[0], parts[1])

# Test case
sample_text = "Introductory content[Chapter 1: Getting Started[Chapter 2: Advanced Topics"
first_part, second_part = split_at_chapter(sample_text)
print("First part:", first_part)
# Output: First part: Introductory content
print("Second part:", second_part)
# Output: Second part: 1: Getting Started[Chapter 2: Advanced Topics

# If you want to retain "[Chapter" at the start of the second part:
def split_at_chapter_with_retain(input_str):
    parts = input_str.split("[Chapter", 1)
    if len(parts) == 1:
        return (parts[0], "")
    return (parts[0], "[Chapter" + parts[1])

first_part, second_part = split_at_chapter_with_retain(sample_text)
print("Second part with marker retained:", second_part)
# Output: Second part with marker retained: [Chapter1: Getting Started[Chapter 2: Advanced Topics

Key Note

Using split("[Chapter", 1) ensures we only split at the first occurrence of the marker, which gives you exactly the two parts you need. Adjust the edge case handling based on whether you want to return empty strings, raise an error, or take other action if the marker is missing.


内容的提问来源于stack exchange,提问作者padrian92

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:07:51