正则表达式在python中输出额外的文本

时间:2018-03-07 14:25:46

标签: python regex

我创建了一个正则表达式来查找1月2日的日期。然而,我的正则表达式也输出了一年,即2007年1月2日。我不确定为什么会这样做。知道我做错了吗?

我的代码:

with open('file1.txt','r') as f_input, open('file2.txt','w') as f_output:

    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)

    dict_month_name =['january','february','march','april','may','june','july','august','september','october','november','december']
    dict_month_prefix =['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']


    for line in csv_input:
        rx = r'\b[0-9]\s+(?:{month_prefix}|{month_name})\b'.format(
        month_prefix = "|".join(dict_month_prefix),
        month_name = "|".join(dict_month_name))
        x = re.findall(rx, line[3], re.I)
        if len(x) != 0:
            csv_output.writerow([line[0], line[1], line[2], line[3], line[4]])

1 个答案:

答案 0 :(得分:0)

你的正则表达式看起来根本不匹配,尝试这样的事情:

(\d+)\s+(jan|feb|mar|apr|may|etc)