为什么我的代码过滤掉了我不要求它过滤的字符? (蟒蛇)

时间:2017-04-25 15:45:31

标签: python regex

我只是要求我的代码过滤掉' - '和'+',但它也过滤了'//'它不应该过滤掉。

任何人都知道为什么会这样做?

with open("some_file_1.txt") as f, open("some_file_2.txt") as g:
    flines = f.readlines()
    glines = g.readlines()

    d = difflib.Differ()
    diff = d.compare(flines, glines)

with open('merged_tp.txt', 'w') as file_out:
    for line in diff:
        print line
        new_line = re.sub('[+-?]', '', line)
        file_out.write(new_line)

1 个答案:

答案 0 :(得分:4)

字符集[+-?]表示" +?"之间的任何字符。

+具有整数序数43. ?具有整数序数63.范围43-63包括以下字符:+,-./0123456789:;<=>?

字符集[+-]表示&#34; +-&#34;,我认为这就是你想要的。