过滤不区分大小写的列表项

时间:2018-04-12 11:27:11

标签: python regex python-3.x

需要帮助来最小化代码。我已经使用RegEx来过滤来自JSON文件的数据以接受(I)teration以及(i)teration。但是在替换具有迭代或迭代的列表项时,我只能替换一个。通过附加结果我得到了所需的输出,但它延伸了这个简单任务的代码。我不确定明天我可能会把迭代视为iTERaTion ..

我的目标是从列表项中提取唯一的数字,将其转换为日期时间格式,我会照顾它。

代码:

print("Unfiltered Folders : \n", Path_List)
regex = re.compile('(?i)^20\d{6}-Iteration$')
regex1 = re.compile('(?i)-iteration$')
Filtered_Path_List = list(filter(regex.search,Path_List))
print("List of Iteration folder will be considered for further Process: \n", 
Filtered_Path_List)

final_iteration = [e.replace("-Iteration", "") for e in Filtered_Path_List]     
print("Latest Iteration list : ", final_iteration)

LOG:

    Unfiltered Folders : 
 ['20171226-Iteration', '20180105-iteration', '20180112-iteration', '20180117-Iteration', '20180118-Iteration', '20180123-Iteration', '20180124-Iteration', '20180202-Iteration', '20180207-Iteration', '20180220-Iteration', '20180321-Iteration', '20180322-Iteration', '20180327-Iteration', '_Kyle-AutomationTesting', '_Template']
List of Iteration folder will be considered for further Process: 
 ['20171226-Iteration', '20180105-iteration', '20180112-iteration', '20180117-Iteration', '20180118-Iteration', '20180123-Iteration', '20180124-Iteration', '20180202-Iteration', '20180207-Iteration', '20180220-Iteration', '20180321-Iteration', '20180322-Iteration', '20180327-Iteration']
Latest Iteration list :  ['20171226', '20180105-iteration', '20180112-iteration', '20180117', '20180118', '20180123', '20180124', '20180202', '20180207', '20180220', '20180321', '20180322', '20180327']
all_iteration.txt file created on : 2018-04-12 16:44

Process finished with exit code 0

正如您所看到的,final_iteration []具有#20120105-iteration' 20180112-iteration'没有过滤效果。

2 个答案:

答案 0 :(得分:1)

试试这个:

re.compile('^20\d{6}(?=-iteration$)', re.IGNORECASE)

这不仅会对正则表达式不区分大小写,而且还会从字符串中仅选择日期部分

答案 1 :(得分:0)

您可以尝试使用我更改的代码来应用不区分大小写的内容。

final_iteration = [re.sub("(?i)-Iteration", "",e) for e in Filtered_Path_List]

我在您的脚本中使用了re.sub()而不是e.replace()

然后iteration在输出中被删除,如下所示

Latest Iteration list :  ['20171226', '20180105', '20180112', '20180117', '20180118', '20180123', '20180124', '20180202', '20180207', '20180220', '20180321', '20180322', '20180327']