Bander
Bander

Reputation: 35

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:

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.

Upvotes: 0

Views: 71

Answers (1)

nishant kumar
nishant kumar

Reputation: 517

dont convert your json to text you could achieve your target very easily. 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)

Upvotes: 1

Related Questions