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

如何用正则表达式仅匹配含['Start']与['End']的Python列表?

解决Python列表正则匹配范围错误的问题

我需要提取文本中以['Start']开头、['End']结尾的Python嵌套列表,但原正则表达式匹配时包含了大量无关文本(比如中间的步骤说明),导致结果不符合预期。

原代码的问题出在正则r"\[\['Start'\].*?\['End'\]\]":.*?虽是非贪婪匹配,但在re.DOTALL模式下会匹配任意字符(包括换行),导致从第一个出现的['Start']开始,一直匹配到后面第一个['End'],把中间所有无关文本都包含进去了。

修改后的正则表达式

要精准匹配符合格式的列表,需要限定中间内容只能是合法的子列表元素,而非任意文本。使用以下正则:

pattern = re.compile(r"\[\['Start'\](?:,\s*\[\[.*?\]\])*,\s*\['End'\]\]", re.DOTALL)

正则说明

  • \[\['Start'\]:匹配列表的起始部分['Start']
  • (?:,\s*\[\[.*?\]\])*:非捕获组,重复匹配「逗号 + 任意空白(含换行) + 子列表」的结构,确保中间只有合法的列表元素
  • ,\s*\['End'\]\]:匹配列表的结尾部分,允许['End']前有任意空白(适配换行缩进的情况)

修改后的完整代码

import re

content = """
Variation 1:
Based on the provided examples and the input sequence, the next anticipated actions for the input sequence [['Start'], ['Pick', 'mixing_bowl_green']] could be:

1. Pick the sponge (since wiping actions typically require the sponge).
2. Wipe the mixing bowl.
3. Place the mixing bowl.
4. Place the sponge.
5. Pick the broom.
6. Sweep.
7. Place the broom.
8. End the sequence.

Here is the completed sequence:

[['Start'], ['Pick', 'mixing_bowl_green'], ['Pick', 'sponge_small'], ['Wipe mixing_bowl_green'], ['Place', 'mixing_bowl_green'], ['Place', 'sponge_small'], ['Pick', 'broom'], ['Sweep'], ['Place', 'broom'], ['End']]

Variation 2:
Based on the provided examples, the anticipated sequence of actions for the given input can be completed as follows:

Input = [['Start'], ['Pick', 'mixing_bowl_green']]

1. The next logical step is to pick the sponge, as it is required for the wiping actions.
2. Then, proceed to wipe the mixing bowl.
3. Place the mixing bowl back.
4. Pick the cutting board and wipe it.
5. Place the cutting board back.
6. Pick the plate and wipe it.
7. Place the plate back.
8. Place the sponge back.
9. Pick the broom and sweep.
10. Place the broom back.
11. End the sequence.

Here is the completed sequence:

[['Start'],
 ['Pick', 'mixing_bowl_green'],
 ['Pick', 'sponge_small'],
 ['Wipe mixing_bowl_green'],
 ['Place', 'mixing_bowl_green'],
 ['Pick', 'cutting_board_small'],
 ['Wipe cutting_board_small'],
 ['Place', 'cutting_board_small'],
 ['Pick', 'plate_dish'],
 ['Wipe plate_dish'],
 ['Place', 'plate_dish'],
 ['Place', 'sponge_small'],
 ['Pick', 'broom'],
 ['Sweep'],
 ['Place', 'broom'],
 ['End']]
"""

# 替换为精准匹配的正则
pattern = re.compile(r"\[\['Start'\](?:,\s*\[\[.*?\]\])*,\s*\['End'\]\]", re.DOTALL)

matches = pattern.findall(content)

for idx, match in enumerate(matches, 1):
    print(f"list{idx} = {match}")

运行结果

执行后会输出两个精准匹配的列表,完全符合预期:

list1 = [['Start'], ['Pick', 'mixing_bowl_green'], ['Pick', 'sponge_small'], ['Wipe mixing_bowl_green'], ['Place', 'mixing_bowl_green'], ['Place', 'sponge_small'], ['Pick', 'broom'], ['Sweep'], ['Place', 'broom'], ['End']]
list2 = [['Start'],
 ['Pick', 'mixing_bowl_green'],
 ['Pick', 'sponge_small'],
 ['Wipe mixing_bowl_green'],
 ['Place', 'mixing_bowl_green'],
 ['Pick', 'cutting_board_small'],
 ['Wipe cutting_board_small'],
 ['Place', 'cutting_board_small'],
 ['Pick', 'plate_dish'],
 ['Wipe plate_dish'],
 ['Place', 'plate_dish'],
 ['Place', 'sponge_small'],
 ['Pick', 'broom'],
 ['Sweep'],
 ['Place', 'broom'],
 ['End']]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 14:55:55