简体   繁体   English

无法使用python re.sub从文本中删除确切的字符串

[英]Unable to remove the exact string from text with python re.sub

I have Following text: 我有以下文字:

{
'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00',  
'devicehandle': '0x0000033c', 
'controlcode': 2228388, 
'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 
'function': 'openfile' 
}

I wanna replace the following part: 我想替换以下部分:

'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00'

with

'inputbuffer':

and

'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

with

'outputbuffer':

I have wrote following python code: 我写了以下python代码:

import codecs
import base64
x1="{'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00',  'devicehandle': '0x0000033c', 'controlcode': 2228388, 'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'function': 'openfile' }"
x3=re.sub(r'(^\w+)','',x1)
x4=re.sub(r'(\<|>)','',x3)
x5=re.sub(r'[^\x00-\x7F]+','', x4)
x6=re.sub(r'(\$|%|\|\(|\)|\\|@|\.|_|-|#|\?)','',x5)
x9=re.sub(r'\'outputbuffer\':\s\'.*\'','\'outputbuffer\':',x6, flags=re.IGNORECASE)
x10=re.sub(r'\'inputbuffer\':\s\'.*\',\s','\'inputbuffer\':',x9, flags=re.MULTILINE)
print(x10)

the desired output should replace only these two parts and keep the others intact as following: 所需的输出应仅替换这两个部分,并保持其余部分不变,如下所示:

{'inputbuffer':,  'devicehandle': '0x0000033c', 'controlcode': 2228388, 'outputbuffer': }

but what I get is : 但是我得到的是:

{'inputbuffer':'controlcode': 2228388, 'outputbuffer': }

which removes some parts that should remain in the resulting text. 删除了一些应保留在结果文本中的部分。

I would be so grateful if somebody help me to figure out what is wrong with this code. 如果有人帮助我弄清楚这段代码有什么问题,我将非常感激。

dont convert your json to text you could achieve your target very easily. 不要将json转换为文本,就可以很容易地实现目标。 just use this code 只需使用此代码

x1 = {'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00',  'devicehandle': '0x0000033c', 'controlcode': 2228388, 'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'function': 'openfile' }
x1[inputbuffer] = ""
x2[outputbuffer] = ""
print(x1)

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

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