Reputation: 381
I have a mixed pattern of characters, numbers and hex characters like the below one,
\x20\x20\x20asdasd\x20\x20\x20asd\x20a12v\x20123123\x20\x20
Using regex, i want to find out only those which are not hex patterns like asdasd, a12v. 123123
E.x : not(\[xX]\d{2})
final End Goal is to convert everything to hex format.
\x20\x20\x20\x61\x73\x64\x61\x73\x64\x20\x20\x20\x61\x73\x64\x20\x61\x31\x32\x76\x20\x31\x32\x33\x31\x32\x33\x20\x20
How to do this ?
Thanks.
Upvotes: 0
Views: 34
Reputation: 1066
You could probably do this with a regex, but it'd likely be quite error prone, as you'd need to have your regex account for all the other escaping possibilities (octal, for example), which turns into rather a mess. :)
Instead, try using str.decode first to un-escape the data:
s = '\\x20\\x20\\x20asdasd\\x20\\x20\\x20asd\\x20a12v\\x20123123\\x20\\x20'
s = s.decode('string-escape')
s = ''.join('\\x%2x' % ord(c) for c in s)
For the last step, there's likely a faster way. but I just threw that together as an example.
Upvotes: 2