简体   繁体   English

Python re.sub不替代

[英]Python re.sub does not replace

I'm trying to replace some fragments in file with code: 我正在尝试用代码替换文件中的一些片段:

#!/usr/bin/python
import re
import binascii

def replace(match):
    nw = hex(int(match.group(2),16)+int('43',16))[-2:]
    nl = match.group(2)+match.group(3)+match.group(4)+nw+match.group(6)
    print(match.group(1),nl,nw,len(match.group(1)),len(nl))
    return nl

def main():
    pattern = re.compile(r'(?=((\w{2})(\w{2}0C)(0202010302)(\w{2})(03)))')
    f = open('txt_dump')

    for line in f:
#        for match in re.finditer(pattern, line):
#            print(match.group(1),match.group(2),match.group(3),match.group(4),match.group(5),match.group(6), hex(int(match.group(2),16)+int('43',16)))
#            nw = hex(int(match.group(3),16)+int('43',16))[-2:]
        a = pattern.subn(repl=replace, string=line)        
    print(a[1])
    with open("out", "wb") as of:
        of.write(binascii.unhexlify(a[0]))

    with open("out_txt", "w") as of:
        of.write(a[0])



if __name__ == '__main__':
    main()

As a result I'm getting such output: 结果,我得到这样的输出:

7D030C02020103020003 7D030C0202010302c003 c0 20 20
00030C02020103020003 00030C02020103024303 43 20 20
A7030C02020103020303 A7030C0202010302ea03 ea 20 20
03030C02020103020303 03030C02020103024603 46 20 20
22030C02020103020303 22030C02020103026503 65 20 20
03030C02020103020303 03030C02020103024603 46 20 20
5A030C02020103020003 5A030C02020103029d03 9d 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020303 00030C02020103024303 43 20 20
03030C02020103020303 03030C02020103024603 46 20 20
4F030C02020103020203 4F030C02020103029203 92 20 20
D5030C02020103020003 D5030C02020103021803 18 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020103 00030C02020103024303 43 20 20
01030C02020103020503 01030C02020103024403 44 20 20
0D030C02020103020003 0D030C02020103025003 50 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020103 00030C02020103024303 43 20 20
01030C02020103020203 01030C02020103024403 44 20 20
02030C02020103020203 02030C02020103024503 45 20 20
CB030C02020103020003 CB030C02020103020e03 0e 20 20
00030C02020103020003 00030C02020103024303 43 20 20
00030C02020103020603 00030C02020103024303 43 20 20
06030C02020103020203 06030C02020103024903 49 20 20
02030C02020103020103 02030C02020103024503 45 20 20
89030C02020103020003 89030C0202010302cc03 cc 20 20
00030C02020103020003 00030C02020103024303 43 20 20
62030C02020103020003 62030C0202010302a503 a5 20 20
32

So I'm sure it found all fragments properly. 因此,我确定它可以正确找到所有碎片。 But in resulting file it does not replace matches, it placing new value near the old. 但是在生成的文件中,它不会替换匹配项,而是将新值放在旧值附近。 For example 7D030C0202010302c0037D030C02020103020003 instead of 7D030C0202010302c003 . 例如7D030C0202010302c0037D030C02020103020003而不是7D030C0202010302c003

What am I doing wrong? 我究竟做错了什么?

In your pattern, one of the group is the whole pattern (?=()) 在您的模式中,组之一是整个模式(?=())

For example for 例如

7D030C0202010302c003 c0 20 20

As a result you get this as a match: 结果,您将其作为匹配项:

('7D030C0202010302c003', '7D', '030C', '0202010302', 'c0', '03') ('7D030C0202010302c003','7D','030C','0202010302','c0','03')

If you remove the outer group your pattern is something like this : 如果删除外部组,则模式如下所示:

pattern = re.compile(r'(\w{2})(\w{2}0C)(0202010302)(\w{2})(03)')

You get this : 你得到这个:

('7D', '030C', '0202010302', 'c0', '03')

And the end result : 最终结果是:

030C0202010302c04f03 c0 20 20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM