| 73 | import re |
| 74 | |
| 75 | def recursive_extract(text): |
| 76 | results = [] |
| 77 | # 定义不同引号类型 |
| 78 | quote_types = ["''", '""', "'", '"'] |
| 79 | remaining_text = text |
| 80 | for quote in quote_types: |
| 81 | # 动态生成正则表达式,排除所有格形式并跳过 'the text :' 前缀 |
| 82 | pattern = ( |
| 83 | r'(?<!\w)' # 确保左侧不是字母(排除所有格) |
| 84 | r'(?:the\s+text\s*:\s*)?' # 可选的 'the text :' 前缀 |
| 85 | r'(?<!\\)(?:\\\\)*' # 允许转义字符(如 \", \') |
| 86 | + re.escape(quote) + |
| 87 | r'(.*?)' # 非贪婪匹配内容 |
| 88 | r'(?<!\\)(?:\\\\)*' # 允许转义字符 |
| 89 | + re.escape(quote) + |
| 90 | r'(?!\w)' # 确保右侧不是字母(排除所有格) |
| 91 | ) |
| 92 | while True: |
| 93 | match = re.search(pattern, remaining_text, re.DOTALL) |
| 94 | if not match: |
| 95 | break |
| 96 | start, end = match.span() |
| 97 | content = match.group(1) |
| 98 | # 递归提取嵌套内容 |
| 99 | nested_results = recursive_extract(content) |
| 100 | if nested_results: |
| 101 | results.extend(nested_results) |
| 102 | else: |
| 103 | results.append(content.strip()) |
| 104 | # 移除已匹配的部分 |
| 105 | remaining_text = remaining_text[:start] + remaining_text[end:] |
| 106 | return results |
| 107 | |
| 108 | all_extracted = recursive_extract(prompt) |
| 109 | final_results = [] |