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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'string.': 0.05; "'',": 0.07; 'subject:would': 0.07; 'string': 0.09; 'assuming': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'python': 0.11; 'fetch': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'shambhu': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'wrote:': 0.18; 'command': 0.22; '>>>': 0.22; 'portion': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'header:X-Complaints-To:1': 0.27; "skip:' 10": 0.31; 'subject:what': 0.31; 'anyone': 0.31; 'skip:d 20': 0.34; 'subject:the': 0.34; 'could': 0.34; 'method': 0.36; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'as:': 0.81; "skip:' 110": 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: what would be the regular expression for null byte present in a string
Date Tue, 13 Jan 2015 15:41:51 +0100
Organization None
References <39F9E8A5546B9448A82E55FBEBE3EC450BD029A5@SACMBXIP03.sdcorp.global.sandisk.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9983.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17677.1421160126.18130.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421160126 news.xs4all.nl 2961 [2001:888:2000:d::a6]:60869
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83698

Show key headers only | 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