Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83698

Re: what would be the regular expression for null byte present in a string

From Peter Otten <__peter__@web.de>
Subject Re: what would be the regular expression for null byte present in a string
Date 2015-01-13 15:41 +0100
Organization None
References <39F9E8A5546B9448A82E55FBEBE3EC450BD029A5@SACMBXIP03.sdcorp.global.sandisk.com>
Newsgroups comp.lang.python
Message-ID <mailman.17677.1421160126.18130.python-list@python.org> (permalink)

Show all headers | View raw


Shambhu Rajak wrote:

> I have a string that I get as an output of a command as:
> 
'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'
> 
> I want to fetch '10232ae8944a' from the above string.
> 
> I want to find a re pattern that could replace all the \x01..\x0z to be
> replace by empty string '',  so that I can get the desired portion of
> string
> 
> Can anyone help me with a working regex for it.

I think you want the str.tranlate() method rather than a regex. 

Assuming you are using Python 2:

>>> delenda = "".join(map(chr, range(32)))
>>> identity = "".join(map(chr, range(256)))
>>> 
'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'.translate(identity, 
delenda)
'10232ae8944a'

With Python3:

>>> mapping = dict.fromkeys(range(32))
>>> 
'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'.translate(mapping)
'10232ae8944a'

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: what would be the regular expression for null byte present in a string Peter Otten <__peter__@web.de> - 2015-01-13 15:41 +0100
  Re: what would be the regular expression for null byte present in a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-14 14:52 +0100
    Re: what would be the regular expression for null byte present in a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-14 15:11 +0100

csiph-web