基于正则与三个列表的自定义模式匹配器开发求助
问题分析
现有代码的核心问题完全偏离了需求逻辑:
- 错误地单独遍历三个列表,没有关联同一索引下的三类信息,破坏了数据的对应关系
- 用list1元素作为字典键,重复值会覆盖之前的关联,导致索引对应错误
- 完全忽略模式中的
#通配规则和顺序匹配要求,只是简单匹配所有符合pattern的元素,逻辑完全不符合需求
重构方案
基于你给出的匹配规则,重新设计的代码如下:
def search_pattern(list1, list2, list3, pattern_list): # 先验证三个列表长度一致 if len(list1) != len(list2) or len(list1) != len(list3): raise ValueError("三个列表长度必须相等") # 将三个列表按索引打包,每个元素对应一组(string, regex, placeholder) items = list(zip(list1, list2, list3)) total_count = len(items) # 处理模式,提取需要匹配的目标节点,标记是否有#通配符 targets = [] has_wildcard = False for p in pattern_list: if p == '#': has_wildcard = True else: targets.append(p) # 特殊情况:模式为空或只有#,返回空列表 if not targets: return [] # 第一步:找到第一个目标的所有可能起始索引 first_target = targets[0] first_indices = [] for idx, (s, r, ph) in enumerate(items): if first_target in (s, r, ph): first_indices.append(idx) if not first_indices: return [] # 第一个目标未匹配到 # 第二步:遍历起始索引,寻找后续目标的匹配位置 for start_idx in first_indices: current_idx = start_idx match_success = True for target in targets[1:]: # 根据是否有#,确定后续搜索的起始位置:#要求中间至少隔一个元素 search_start = current_idx + 1 if has_wildcard else current_idx found_idx = None for idx in range(search_start, total_count): s, r, ph = items[idx] if target in (s, r, ph): found_idx = idx break if found_idx is None: match_success = False break current_idx = found_idx if match_success: # 收集从起始到结束索引的所有list1元素 return list1[start_idx:current_idx+1] # 所有可能的起始位置都未匹配到完整模式 return [] # 测试示例 list1 = ['string1', 'string2', 'string1', 'string3'] list2 = ['regex1', 'regex2', 'regex1', 'regex3'] list3 = ['placeholder1', 'placeholder2', 'placeholder1', 'placeholder3'] pattern_list = ['regex2', '#', 'string3'] result = search_pattern(list1, list2, list3, pattern_list) print(result) # 输出: ['string2', 'string1', 'string3']
代码逻辑说明
- 列表打包:用
zip将三个列表按索引绑定,确保每个位置的三类信息对应正确 - 模式处理:提取模式中的非通配目标,标记是否存在
#通配符 - 顺序匹配:先找到第一个目标的所有可能起始索引,再依次寻找后续目标的位置,确保索引递增,且
#要求中间至少有一个元素 - 结果收集:匹配成功后,返回从第一个匹配索引到最后一个匹配索引之间的所有list1元素,完全符合示例预期
扩展说明
- 如果需要支持多个
#(比如['a', '#', 'b', '#', 'c']),只需调整模式处理逻辑,将模式拆分为多个段依次匹配即可 - 如果需要返回所有符合条件的结果(而非第一个匹配的),可以将结果收集逻辑改为append到列表,最后返回所有匹配的结果集
内容的提问来源于stack exchange,提问作者Laz22434
相关产品推荐
相关产品推荐

